在新列中转换json内容

2024-05-15 05:23:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带有semi structured data的数据集,我需要转换其他列中content列中的json

数据:

    customer    flow    session timestamp               content
1   C1000   F1000   S2000   2019-12-16 13:59:58+00:00   {'name': ''}
2   C1000   F1000   S2000   2019-12-16 13:59:59+00:00   {'name': 'joao'}
4   C1000   F1000   S2000   2019-12-16 13:59:59+00:00   {'cpf': '733.600.420-26'}

预期结果如下所示:

+--------+-----+-------+-------------------+-------------------+-----+--------------+------------------+
|customer|flow |session|first_answer_dt    |last_answer_dt     |name |cpf           |delivery_confirmed|
+--------+-----+-------+-------------------+-------------------+-----+--------------+------------------+
|C1000   |F1000|S1000  |2019-12-16T13:59:58|2019-12-16T14:00:01|maria|305.584.960-40|sim               |
|C1000   |F1000|S2000  |2019-12-16T13:59:59|2019-12-16T14:00:00|joao |733.600.420-26|não               |
+--------+-----+-------+-------------------+-------------------+-----+--------------+------------------+

我在网上搜索,但很难找到解决这个问题的办法


Tags: 数据answernamesessiondtcustomercontentflow
1条回答
网友
1楼 · 发布于 2024-05-15 05:23:33

IIUC,你可以试试.joinpd.Series

#use eval if your json is a string.
df1 = df.join(df['content'].map(eval).apply(pd.Series)).drop('content',axis=1)
#or if not string
df1 = df.join(df['content'].apply(pd.Series)).drop('content',axis=1)
print(df1)
  customer   flow session                 timestamp  name             cpf
0    C1000  F1000   S2000 2019-12-16 13:59:58+00:00                   NaN
1    C1000  F1000   S2000 2019-12-16 13:59:59+00:00  joao             NaN
2    C1000  F1000   S2000 2019-12-16 13:59:59+00:00   NaN  733.600.420-26

相关问题 更多 >

    热门问题