【问题标题】:Using a for loop to reduce the number of lines of code使用 for 循环减少代码行数
【发布时间】:2021-03-18 13:33:00
【问题描述】:

我有一个DataFrame,看起来像这样

df=
Rank   Date  Age Name  Score
3      9001  23  Lilly  40
2      9002  23  Lilly  45
2      8001  19  Tom    80
3      8010  19  Tom    75
1      4040  28  Cindy  85
3      4041  28  Cindy  50
4      3800  37  Don    35
4      3900  38  Don    38

我要做的是将separate dictionarieskey 作为"Name" 列和来自"Rank, Date, Age and Score" 列值的value 字段。它应该是这样的

{ 'Lilly': [3,2] }
{ 'Lilly': [9001,9002] }
{ 'Lilly': 23 }
{ 'Lilly': [40,45] }
------
{ 'Don': [35,38] }

我想使用for loop 代替我使用的干代码,其中包含大量重复的代码行

list_1 = df['Rank'].tolist()
list_2 = df['Date'].tolist()
list_3 = df['Age'].tolist()
list_4 = df['Name'].tolist()
list_5 = df['Score'].tolist()

dict_1 = {list_4[i]: list_1[i] for i in range(len(list_1))}
dict_2 = {list_4[i]: list_2[i] for i in range(len(list_1))}
dict_3 = {list_4[i]: list_3[i] for i in range(len(list_1))}
dict_4 = {list_4[i]: list_5[i] for i in range(len(list_1))}

是否可以在 for loop 内创建单独的 numbered dictionaries 而不必编写重复的代码行?

【问题讨论】:

  • 简短的回答是否定的。您不能在循环内创建新的变量名。解决这个问题的方法是使用字典来存储字典。

标签: python dataframe dictionary dynamic


【解决方案1】:

如果你想创建这样的东西

[{'Lilly': 2, 'Tom': 3, 'Cindy': 3, 'Don': 4},
 {'Lilly': 9002, 'Tom': 8010, 'Cindy': 4041, 'Don': 3900},
 {'Lilly': 23, 'Tom': 19, 'Cindy': 28, 'Don': 38},
 {'Lilly': 45, 'Tom': 75, 'Cindy': 50, 'Don': 38}]

使用 for 循环,您可以在数据框下使用此代码块,而不是这些重复的代码块:

list_of_dicts=[]
name_list = df['Name'].tolist()
for key in df.keys():
    if key == 'Name': continue
    else:
        templist = df[key].tolist()
        tempdict = {name_list[i]: templist[i] for i in range(len(list_1))}
        list_of_dicts.append(tempdict)
list_of_dicts

【讨论】:

    【解决方案2】:

    一般来说,我建议不要将它们分开存储,而是存储在字典本身中。 这就是在 python for 循环中进行变量赋值的方式How do you create different variable names while in a loop?

    然后我会使用一个简单的 for 循环来遍历您的 df 列:

    for column in df:
        print(df[column])
    

    【讨论】:

      【解决方案3】:

      试试这个:

      groups = df.groupby('Name')
      lst = []
      for c in df.columns:
          if not c=='Name': lst = lst + [{k:list(v.values())} for k,v in 
              pd.DataFrame(groups[c].apply(list)).to_dict('index').items()]
      

      【讨论】:

        猜你喜欢
        • 2021-07-19
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-22
        • 2011-10-29
        相关资源
        最近更新 更多