Python 如何将数据帧列对转换为单个字典映射?

Python 如何将数据帧列对转换为单个字典映射?,python,pandas,dataframe,dictionary,Python,Pandas,Dataframe,Dictionary,如何将数据帧值转换为字典 D_ID Designation G_Code G 1 Developer 1 M 1 Developer 2 F 1 Developer 1 M 2 Testing 1 M 2 Testing 1 M 2 Testing 2 F 预料之外 d1={1:“开发人员”,2:“测试”}

如何将数据帧值转换为字典

D_ID    Designation  G_Code G

1       Developer       1   M
1       Developer       2   F
1       Developer       1   M
2       Testing         1   M
2       Testing         1   M
2       Testing         2   F
预料之外

d1={1:“开发人员”,2:“测试”}


d2={1:“M”,2:“F”}

如果您事先知道哪些列需要配对,您可以创建一个pairs dict并非常整齐地对其进行迭代:

pairs = {'D_ID': 'Designation', 'G_Code': 'G'}
[df.set_index(k)[v].to_dict() for k, v in pairs.items()]
# [{1: 'Developer', 2: 'Testing'}, {1: 'M', 2: 'F'}]

如果只有两对,则可以将结果分配给“d1”和“d2”:


您可以使用带有参数
orient='split'
的函数
to_dict
将数据帧转换为字典

df[['D_ID', 'Designation']].to_dict('split')
输出:

{'index': [0, 1, 2, 3, 4, 5],
 'columns': ['D_ID', 'Designation'],
 'data': [[1, 'Developer'],
  [1, 'Developer'],
  [1, 'Developer'],
  [2, 'Testing'],
  [2, 'Testing'],
  [2, 'Testing']]}
[{1: 'Developer', 2: 'Testing'}, {1: 'M', 2: 'F'}]
然后,您可以从生成的字典中选择
'data'
,并使用功能
dict
将其转换为另一个字典

dct = {'D_ID': 'Designation', 'G_Code': 'G'}
[dict(df[[k, v]].to_dict('split')['data']) for k, v in dct.items()]
输出:

{'index': [0, 1, 2, 3, 4, 5],
 'columns': ['D_ID', 'Designation'],
 'data': [[1, 'Developer'],
  [1, 'Developer'],
  [1, 'Developer'],
  [2, 'Testing'],
  [2, 'Testing'],
  [2, 'Testing']]}
[{1: 'Developer', 2: 'Testing'}, {1: 'M', 2: 'F'}]

作为可接受的答案的替代,您可以考虑使用<代码> GROPPEB/<代码>,<代码>第一< /代码>,最后<代码> TooDigt < /C> > /P>

dct = {'D_ID': 'Designation', 'G_Code': 'G'}
[dict(df[[k, v]].to_dict('split')['data']) for k, v in dct.items()]
d1=df.groupby(“D_ID”)[“Designation”].first().to_dict()
d2=df.groupby(“G_代码”)[“G”].first().to_dict()
或者最终使用@cs95建议的配对

pairs={'D_ID':'Designation','G_code':'G'}
d1,d2=(df.groupby(k)[v].first().to_dict()表示k,v成对。items())
时机 @cs95
%%timeit
pairs={'D_ID':'Designation','G_Code':'G'}
d1,d2=(df.set_index(k)[v].to_dict()表示k,v成对。items())
每个回路1.06 ms±23.5µs(7次运行的平均值±标准偏差,每个1000个回路)
子句
%%timeit-n 100
d1=df.groupby(“D_ID”)[“指定”].first().to_dict()
d2=df.groupby(“G_代码”)[“G”].first().to_dict()
每个回路1.29 ms±15.6µs(7次运行的平均值±标准偏差,每个1000个回路)
groupby
+对
%%timeit-n 100
pairs={'D_ID':'Designation','G_Code':'G'}
d1,d2=(df.groupby(k)[v].first().to_dict()表示k,v成对。items())
每个回路1.4 ms±24.4µs(7次运行的平均值±标准偏差,每个1000个回路)
结论 在本例中,@cs95 solution显然是赢家。但我想知道哪一个是你的真实案例的时机