开发者

How to add new column to dataframe from dataframe dictionary column

I want to add columns to my dataframe based on the dictionary values which I have inside a dataframe column

attendanceDetails
"[{'clockIn': '1669435507', 'clockOut': '1669468193', 'actionBy'开发者_如何转开发: '', 'actionByName': '', 'actionById': '', 'actionRemark': '', 'actionByTimeStamp': None, 'deviceNameClockIn': '119', 'deviceNumberClockIn': '', 'deviceLocationIdClockIn': '', 'deviceLocationClockIn': '8th', 'deviceNameClockOut': '', 'deviceNumberClockOut': '', 'deviceLocationIdClockOut': '', 'deviceLocationClockOut': '8th'}]"

I want to extract attendancedDetails data into a new column based on the key

Please help me with this


Use:

import ast

df = df.join(pd.json_normalize(df['attendanceDetails'].apply(ast.literal_eval).str[0]))
print (df)
                                   attendanceDetails     clockIn    clockOut  \
0  [{'clockIn': '1669435507', 'clockOut': '166946...  1669435507  1669468193   

  actionBy actionByName actionById actionRemark actionByTimeStamp  \
0                                                            None   

  deviceNameClockIn deviceNumberClockIn deviceLocationIdClockIn  \
0               119                                               

  deviceLocationClockIn deviceNameClockOut deviceNumberClockOut  \
0                   8th                                           

  deviceLocationIdClockOut deviceLocationClockOut  
0                                             8th  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜