从pandas嵌套框架创建嵌套字典

nc1teljy  于 2024-01-04  发布在  其他
关注(0)|答案(5)|浏览(143)

我有这张table:
| 团队|X或Y|百分比|
| --|--|--|
| 一|X|百分之八十|
| 一|Y|百分之二十|
| B| X|百分之七十|
| B| Y|百分之三十|
| C| X|百分之六十|
| C| Y|百分之四十|
我想创建一个嵌套的字典,这样如果我输入球队名称和X或Y,我会得到百分比作为返回值。
在Python中,我使用.tolist()方法来创建每个列的列表。
我最初的策略是先从后两列dict_1 = dict(zip(list2, list3))dict_2 = dict(zip(list1, dict_1))中创建一个dict,但这并没有成功,因为列“X或Y”有类似的值,而字典键不能有重复的值。
我想的输出是

  1. {'A':{'X':80%, 'Y':20%}, 'B':{'X':70%,'Y':30%}, ...}

字符串
我该怎么做呢?有更好的方法吗?

qni6mghb

qni6mghb1#

使用pd.DataFrame.pivot

  1. >>> df.pivot(columns='Team', index='X or Y', values='Percentage').to_dict()
  2. {'A': {'X': '80%', 'Y': '20%'}, 'B': {'X': '70%', 'Y': '30%'}, 'C': {'X': '60%', 'Y': '40%'}}

字符串

92vpleto

92vpleto2#

完成此任务的最快方法是使用itertuples()遍历该框架并动态创建字典。

  1. result = {}
  2. for Team, XorY, Percentage in df.itertuples(index=False):
  3. result.setdefault(Team, {})[XorY] = Percentage

字符串
其中result现在变成期望值:

  1. {'A': {'X': '80%', 'Y': '20%'},
  2. 'B': {'X': '70%', 'Y': '30%'},
  3. 'C': {'X': '60%', 'Y': '40%'}}


一个更“pandas”的代码可以在groupby中调用to_dict

  1. result = (
  2. df.groupby('Team')
  3. .apply(lambda g: g.set_index('X or Y')['Percentage'].to_dict())
  4. .to_dict()
  5. )


这比itertuples循环慢。
不完全相同,但this answer也从一个嵌套对象构造一个嵌套对象,并包含一个基准。

展开查看全部
blpfk2vs

blpfk2vs3#

解决方案1

一个可能的解决方案,使用pandas.stack,然后是pandas.unstack

  1. (df.set_index(['Team', 'X or Y'])
  2. .stack().droplevel(2).unstack('X or Y').T.to_dict())

字符串
@cottontail在下面的评论中建议,这个解决方案的一个更短,更有效的版本:

  1. df.set_index(['Team', 'X or Y'])['Percentage'].unstack('Team').to_dict()

解决方案2

另一种可能的解决方案是使用groupby.apply来构造字典:

  1. (df.groupby('Team').apply(lambda x:
  2. {'X': x.loc[x['X or Y'].eq('X'), 'Percentage'].iloc[0],
  3. 'Y': x.loc[x['X or Y'].eq('Y'), 'Percentage'].iloc[0]})
  4. .to_dict())

输出

  1. {'A': {'X': '80%', 'Y': '20%'},
  2. 'B': {'X': '70%', 'Y': '30%'},
  3. 'C': {'X': '60%', 'Y': '40%'}}

展开查看全部
798qvoo8

798qvoo84#

循环所有列(使用zip()函数):

  1. nd = {} # nested dict
  2. for team, xy, percentage in zip(data['Team'], data['X or Y'], data['Percentage']):
  3. if team not in nd:
  4. nd[team] = {}
  5. nd[team][xy] = percentage

字符串
示例代码:

  1. import pandas as pd
  2. data = {
  3. 'Team': ['A', 'A', 'B', 'B', 'C', 'C'],
  4. 'X or Y': ['X', 'Y', 'X', 'Y', 'X', 'Y'],
  5. 'Percentage': ['80%', '20%', '70%', '30%', '60%', '40%']
  6. }
  7. df = pd.DataFrame.from_dict(data)
  8. print(df)
  9. nd = {} # nested dict
  10. for team, xy, percentage in zip(data['Team'], data['X or Y'], data['Percentage']):
  11. if team not in nd:
  12. nd[team] = {}
  13. nd[team][xy] = percentage
  14. print(nd)


DF和嵌套Dict输出:

  1. Team X or Y Percentage
  2. 0 A X 80%
  3. 1 A Y 20%
  4. 2 B X 70%
  5. 3 B Y 30%
  6. 4 C X 60%
  7. 5 C Y 40%
  8. {'A': {'X': '80%', 'Y': '20%'}, 'B': {'X': '70%', 'Y': '30%'}, 'C': {'X': '60%', 'Y': '40%'}}

展开查看全部
w8f9ii69

w8f9ii695#

我回答了你的最终目标,你想“输入球队名称和输入X或Y,[和]得到的百分比作为返回值”,但输出将看起来与你上面建议的不同。如果这对你不起作用,请忽略这个答案。
我会重新索引表,将输入作为索引,然后从那里获取字典,所以:

  1. import pandas as pd
  2. data = {
  3. 'Team': ['A', 'A', 'B', 'B', 'C', 'C'],
  4. 'X or Y': ['X', 'Y', 'X', 'Y', 'X', 'Y'],
  5. 'Percentage': ['80%', '20%', '70%', '30%', '60%', '40%']
  6. }
  7. df = pd.DataFrame.from_dict(data)
  8. df = df.set_index(['Team', 'X or Y'])
  9. df1.to_dict()
  10. {'Percentage': {('A', 'X'): '80%', ('A', 'Y'): '20%', ('B', 'X'): '70%', ('B', 'Y'): '30%', ('C', 'X'): '60%', ('C', 'Y'): '40%'}}

字符串

相关问题