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
精彩评论