【问题标题】:How to add new column to dataframe from dataframe dictionary column如何从数据框字典列向数据框添加新列
【发布时间】:2022-12-18 14:05:16
【问题描述】:
我想根据数据框列中的字典值向我的数据框添加列
| attendanceDetails |
| "[{'clockIn': '1669435507', 'clockOut': '1669468193', 'actionBy': '', 'actionByName': '', 'actionById': '', 'actionRemark': '', 'actionByTimeStamp': None, 'deviceNameClockIn': '119', 'deviceNumberClockIn': '', 'deviceLocationIdClockIn': '', 'deviceLocationClockIn': '8th', 'deviceNameClockOut': '', 'deviceNumberClockOut': '', 'deviceLocationIdClockOut': '', 'deviceLocationClockOut': '8th'}]" |
我想根据键将 attendancedDetails 数据提取到新列中
请在这件事上给予我帮助
【问题讨论】:
标签:
python-3.x
pandas
dataframe
dictionary
data-analysis
【解决方案1】:
利用:
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