我有一个python字典,如下所示:
d = {'comp_1': {'property_4': 24, 'property_2': 45, 'property_3': 124, 'missing': 39, 'property_1': 16},
'comp_2': {'property_4': 23, 'property_2': 49, 'property_3': 126, 'property_1': 16, 'missing': 38},
'comp_3': {'property_4': 24, 'property_2': 43, 'property_1': 19, 'missing': 30, 'property_3': 116}}
当我将其加载到熊猫数据框并尝试打印它时,它看起来如下:
df = pd.DataFrame.from_dict(hits, orient='index')
print(df)
输出:
missing property_1 property_2 property_3 property_4
comp_1 39 16 45 124 24
comp_2 38 16 49 126 23
comp_3 30 19 43 116 24
现在,我想重命名列,所以我尝试:
df = pd.DataFrame.from_dict(hits, orient='index' columns=reversed(['Missing', 'P1', 'P2', 'P3', 'P4']))
产生空数据帧(我假设因为字典中不存在这些键?):
Empty DataFrame
Columns: []
Index: []
如果我尝试这样做:
df = pd.DataFrame.from_dict(hits, orient='index')
columns = reversed(['Missing', 'P1', 'P2', 'P3', 'P4'])
df.columns=columns
按顺序重命名的列不会保留,因此每次运行代码时,该数字都不对应于列,例如:
P4 P3 P2 P1 Missing
comp_1 16 24 124 45 39
comp_2 16 23 126 49 38
comp_3 19 24 116 43 30
和:
P4 P3 P2 P1 Missing
comp_1 24 16 39 124 45
comp_2 23 16 38 126 49
comp_3 24 19 30 116 43
当我将数据加载到数据帧中时,我猜我需要以某种方式从嵌套的dictioary中提供键,但我不知道该怎么做。还是我需要做的其他事情?
编辑: 我也尝试用字典重命名列,如下所示:
df.rename({'missing': 'Missing', 'property_1': 'P1', 'property_2': 'P2', 'property_3': 'P3',
'property_4': 'P4'})
解决办法:columns参数in to_dict仅指定要选择的列。例如,
pd.DataFrame.from_dict(hits, orient='index', columns=['property_4'])
property_4
comp_1 24
comp_2 23
comp_3 24
只选择“property_4”列,忽略其他所有内容。当然,这是有道理的,因为字典本身没有排序。您唯一的选择是重命名密钥或使用重命名列DataFrame.rename()。
cmap = {'property_1': 'P1', 'property_2': 'P2', 'property_3': 'P3',
'property_4': 'P4', 'missing': 'Missing'}
df = df.rename(columns=cmap)
df
P4 P2 P3 Missing P1
comp_1 24 45 124 39 16
comp_2 23 49 126 38 16
comp_3 24 43 116 30 19








暂无数据