【发布时间】:2022-01-03 18:06:57
【问题描述】:
我需要将列中带有 json 值的以下数据框转换为数据框列式结构,以便占用更少的空间并易于计算。
示例数据帧:
| obs_id | date | obs |
|---|---|---|
| I2213 | 2021-12-31 23:20:02.761008 | "[{'type': 'air', 'results': {'bat': {'F1': 0.1, 'F2': 0.2}}, {'type': 'water', 'results': {'neo': {'F1': 0.3}}]" |
| I2213 | 2022-01-01 23:20:02.761008 | "[{'type': 'earth', 'results': {'cat': {'F1': 0.4}}]" |
| I2213 | 2022-01-02 23:20:02.761008 | "[{'type': 'air', 'results': {'bat': {'F1': 0.2, 'F2': 0.1}}]" |
所需的转换格式:
| obs_id | date | obs.air.bat.F1 | obs.air.bat.F2 | obs.water.neo.F1 | obs.earth.cat.F1 |
|---|
不确定多级列是否更适合这里。
我尝试从 obs 列创建一个单独的数据框,例如:
df1 = pd.DataFrame(df['obs'].values.tolist())
但由于它包含列表而不是字典,所以它不起作用。 能不能达到要求的格式?
【问题讨论】:
-
obs 是字符串?
标签: python json pandas dataframe