开发者

How to add new column to dataframe from dataframe dictionary column

开发者 https://www.devze.com 2022-12-07 20:00 出处:网络
I want to add columns to my dataframe based on the dictionary values which I have inside a dataframe 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

精彩评论

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