我正在尝试将字典转换为数据帧。
import pandas as pd
dict = {'A': [1,2,3], 'B': [1,2,3,4])
pd.DataFrame.from_dict(dict, orient = 'index').T
:
A B
0 [1,2,3] [1,2,3,4]
却得到了:
A B
-----------
0 1 a
1 2 b
2 3 c
3 None d
尝试将字典放入list ([]
):
import pandas as pd
dct = {"A": [1, 2, 3], "B": [1, 2, 3, 4]}
df = pd.DataFrame([dct])
print(df)
打印:
A B
0 [1, 2, 3] [1, 2, 3, 4]
注意:变量名不要使用dict
等保留字