尝试将object
的{{1}}类型列转换为实际需要的dataframe
列表。
dicts
是否可以将print df['A'].dtype
object
print df['A'][:1]
[{"at": "con", "c": 47}, {"at": "cli", "z": 47}, {"at": "cks", "d": 5}]
作为df['A']
的列表检索为dicts
?任何帮助高度赞赏。
答案 0 :(得分:2)
使用json
import json
df.A.apply(json.loads)
0 [{u'c': 47, u'at': u'con'}, {u'z': 47, u'at': ...
1 [{u'c': 47, u'at': u'con'}, {u'z': 47, u'at': ...
2 [{u'c': 47, u'at': u'con'}, {u'z': 47, u'at': ...
3 [{u'c': 47, u'at': u'con'}, {u'z': 47, u'at': ...
4 [{u'c': 47, u'at': u'con'}, {u'z': 47, u'at': ...
5 [{u'c': 47, u'at': u'con'}, {u'z': 47, u'at': ...
Name: A, dtype: object
df = pd.DataFrame([
'[{"at": "con", "c": 47}, {"at": "cli", "z": 47}, {"at": "cks", "d": 5}]',
'[{"at": "con", "c": 47}, {"at": "cli", "z": 47}, {"at": "cks", "d": 5}]',
'[{"at": "con", "c": 47}, {"at": "cli", "z": 47}, {"at": "cks", "d": 5}]',
'[{"at": "con", "c": 47}, {"at": "cli", "z": 47}, {"at": "cks", "d": 5}]',
'[{"at": "con", "c": 47}, {"at": "cli", "z": 47}, {"at": "cks", "d": 5}]',
'[{"at": "con", "c": 47}, {"at": "cli", "z": 47}, {"at": "cks", "d": 5}]',
],
columns=['A'])