【发布时间】:2020-06-12 11:45:14
【问题描述】:
我有一个这样的数据框:
| col1 | d
-------------------------------------------
0 | A | {'student99': [[0.83, "nice"]]}
1 | B | {'student99': [[0.84, "great"], [0.89, "cool"]]}
2 | C | {'student98': [[0.85, "amazing"]], 'student97': [[0.9, "crazy"]]}
我正在尝试转换为如下数据框:
| col1 | student | grade | comment
---------------------------------------------
0 | A | student99| 0.83 | nice
1 | B | student99| 0.84 | great
2 | B | student99| 0.89 | cool
3 | C | student98| 0.85 | amazing
4 | C | student97| 0.9 | crazy
如您所见,我需要将 d 列拆分为 student 、 grade 和 comment 列,并且我需要根据 d 列中的键数将行拆分为一些行(如上面的 C 行)和每个键的列表数(如上面的 B 行)。
我该怎么做?
在评论之后,我注意到数据以 JSON 格式以下一种格式到达(我将其转换为数据框):
{"A": {"d" : {'student99': [[0.83, "nice"]]}},
"B": {"d" : {'student99': [[0.84, "great"], [0.89, "cool"]]},
"C": {"d" : {'student98': [[0.85, "amazing"]], 'student97': [[0.9, "crazy"]]}
}
【问题讨论】:
-
您从哪里获得数据?也许在上游修复它会更容易。
-
我得到它作为 json 文件。我会在上面附上他的结构。
标签: python pandas dictionary