python 数据框到字典,键值问题
python dataframe to dictionary, key value issue
我正在创建一个数据框,然后转换为如下字典
data = {'ID': [1,2,3,4,5],
'A':['1','2','1','3','2'],
'B':[4,6,8,2,4]}
frame = pd.DataFrame(data)
dict_obj = dict(frame[['A','B']].groupby('A').median().sort_values(by='B'))
我的问题是我想将 A 列作为键,将 B 列作为值,但不知何故我得到了一个奇怪的字典
dict_obj
{'B': A
3 2
2 5
1 6
Name: B, dtype: int64}
我想要字典对象作为
{1:6,2:5,3:2}
有人可以帮忙吗?
frame.groupby('A').B.median().to_dict()
{'1': 6, '2': 5, '3': 2}
我正在创建一个数据框,然后转换为如下字典
data = {'ID': [1,2,3,4,5],
'A':['1','2','1','3','2'],
'B':[4,6,8,2,4]}
frame = pd.DataFrame(data)
dict_obj = dict(frame[['A','B']].groupby('A').median().sort_values(by='B'))
我的问题是我想将 A 列作为键,将 B 列作为值,但不知何故我得到了一个奇怪的字典
dict_obj
{'B': A
3 2
2 5
1 6
Name: B, dtype: int64}
我想要字典对象作为
{1:6,2:5,3:2}
有人可以帮忙吗?
frame.groupby('A').B.median().to_dict()
{'1': 6, '2': 5, '3': 2}