【发布时间】:2021-04-13 14:29:29
【问题描述】:
需要一些帮助“解包”CSV 列中的不规则嵌套 JSON 数组。
我想展平此列并将其作为普通平面表附加到数据集的其余部分。
-
当我加载 pandas 数据框时,该列中有一堆 \n:read_csv df output
-
使用read_json,我仍然得到这些\n的read json output
-
在常规数据帧上尝试 read_json 时 df = pd.read_json(df_data, lines=True)。我得到一个 ValueError:解码数组值时发现意外字符 (2)
架构:['time','event_type','user_id','nested_data']
CSV 中的nested_data 列:
{
"message": {
"event": {
"clientSessionId": "c6411c02-f361-4f0e-893f-d180e9b74a5d",
"details": {
"network": {
"effectiveType": "4g"
}
我尝试过的其他一些事情:
flat = flatten_json(df_data.to_dict()) pd.json_normalize(flat) --> 将列变成单行
json.dumps(df['DATA']) --> TypeError: Object of type Series is not JSON serializable
df_data_normal = pd.json_normalize(df_data.to_json) -->TypeError: 'method' object is not iterable
我什至followed the instructions here 但没有运气:
【问题讨论】:
-
欢迎来到 SO Coder_bob!能否请您分享代码以向我们展示您尝试过的内容以及无效的内容?
-
df_data_normal = pd.json_normalize(df_data.to_json) -->TypeError: 'method' object is not iterable.
-
json.dumps(df['DATA']) --> TypeError: Object of type Series is not JSON serializable
-
flat = flatten_json(df_data.to_dict()) pd.json_normalize(flat) --> 将列变成单行
-
错误
TypeError: 'method' object is not iterable告诉您您传入了一个“方法”,在本例中为to_json。您可能想通过添加括号来调用它。例如df_data.to_json()