我在表格/数据框中有一个得分比例。我想读取读取帧并将其转换为多个dict对象。 我有桌子:
col_ref col_cutoff col_value
C1 10 100
C1 20 200
C1 Miss 500
C1 Null 100
C2 A 250
C2 B 200
C2 Null 0
C2 Miss 100
我想将其转换为2个dict对象:
C1_Dict = {
'Miss' : 500,
'Null' :100,
'VALS' : [
(10, 100),
(20, 2000)
]
}
C2_Dict = {
'Miss' : 100,
'Null' :0,
'VALS' : [
(A, 250),
(B, 200)
]
}
以下是我的开始......但我无法进一步了解
import pandas as pd
def panda_2_dict(pd):
pdf_ref = pd.read_csv()
col_refs = pd.pdf_ref.[Col_ref.distinct]
For each i in col_refs
col_ref_i = {col_cutoff:col_val}
return col_ref_i
我期待一个dict对象列表(这里是2)。
答案 0 :(得分:1)
您可以尝试:
col_ref col_cutoff col_value
0 C1 10 100
1 C1 20 200
2 C1 Miss 500
3 C1 Null 100
4 C2 A 250
5 C2 B 200
6 C2 Null 0
7 C2 Miss 100
gb = df.groupby('col_ref')
for k, v in gb:
print k
a = (v[:2].set_index('col_cutoff')['col_value'].to_dict()).items()
b = v[2:].set_index('col_cutoff')['col_value'].to_dict()
b['VALS'] = a
print b
C1
{'VALS': [('10', 100), ('20', 200)], 'Null': 100, 'Miss': 500}
C2
{'VALS': [('A', 250), ('B', 200)], 'Null': 0, 'Miss': 100}