如何将带有列表的字典转换为具有默认索引和列名称的数据帧



如何将字典转换为具有默认索引和列名的数据帧

字典d = {0: [1, 'Sports', 222], 1: [2, 'Tools', 11], 2: [3, 'Clothing', 23]}

df
id   type    value
0  1   Sports   222
1  2   Tools    11
2  3   Clothing 23

DataFrame.from_dictorient='index'参数一起使用:

d = {0: [1, 'Sports', 222], 1: [2, 'Tools', 11], 2: [3, 'Clothing', 23]}
df = pd.DataFrame.from_dict(d, orient='index', columns=['id','type','value'])
print (df)
id      type  value
0   1    Sports    222
1   2     Tools     11
2   3  Clothing     23

最新更新