我们如何将 Pandas 核心系列转换为单独且不同的列?

问题描述 投票:0回答:0

我正在标准化一个 JSON 响应,就像这样。

df = pd.json_normalize(response, record_path=['data'])

现在,如果我运行这个:

print(type(df['locations']))

我得到这个结果:

<class 'pandas.core.series.Series'>

所以,

'df['locations']'
由以下组成:

df['locations']
Out[246]: 
0      [{'time': '2023-01-01T06:49:49Z', 'latitude': ...
1      [{'time': '2023-01-01T01:03:35Z', 'latitude': ...
2      [{'time': '2023-01-01T02:30:45Z', 'latitude': ...
3      [{'time': '2023-01-01T06:38:42Z', 'latitude': ...
4      [{'time': '2023-01-01T01:02:19Z', 'latitude': ...

现在,如果我尝试这个:

df = pd.DataFrame([df['locations']])

我得到这个结果:

[1 rows x 196 columns]

我试着摆弄绳子,像这样……

df_final = df.iloc[1:100].to_string()
df_final = pd.DataFrame(df_final)

我收到一条错误消息:

ValueError: DataFrame constructor not properly called!

如何将 df['locations'] 规范化为 196 个独立且不同的列?这似乎是这里的问题。

python python-3.x dataframe series
© www.soinside.com 2019 - 2024. All rights reserved.