首页 > 解决方案 > 遍历一列并根据 PANDAS 数据框中另一列的值将值添加到列表

问题描述

我有一个遵循此屏幕截图的数据框:

在此处输入图像描述

我想编写一个脚本,将值添加到列表中df['value']。添加值的列表取决于月份数。所以预期的输出是:

jan = [2345]
feb = [435]
mar = [976,76]
apr = [65,55,33]
may = [61]
jun = [658]
jul = [65]
nov = [3]
dec = [56]

真正的df更复杂,但问题是可以转移的。我写了这个脚本没有运气:

    jan = []
    feb = []
    mar = []
    apr = []
    may = []
    jun = []
    jul = []
    aug = []
    sep = []
    octo = []
    nov = []
    dec = []
    for ind,i in yr18_df.iterrows():
          if i == 1:
            jan.append(yr18_df.points)
        else i == 2:
            feb.append(yr18_df.points)
        elif yr18_df.date_month == 3:
            mar.append(yr18_df.points)
        elif yr18_df.date_month == 4:
            apr.append(yr18_df.points)
        elif yr18_df.date_month == 5:
            may.append(yr18_df.points)
        elif yr18_df.date_month == 6:
            jun.append(yr18_df.points)
        elif yr18_df.date_month == 7:
            jul.append(yr18_df.points)
        elif yr18_df.date_month == 8:
            aug.append(yr18_df.points)
        elif yr18_df.date_month == 9:
            sep.append(yr18_df.points)
        elif yr18_df.date_month == 10:
            octo.append(yr18_df.points)
        elif yr18_df.date_month == 11:
            nov.append(yr18_df.points)
        else:
            dec.append(yr18_df.points)

标签: pythonpandas

解决方案


可能像这样:

考虑下面的示例数据框:

In [2368]: df = pd.DataFrame({'value':[2345,123,282,367,213], 'month':[1,2,9,1,2]})                                                                                                                         

In [2369]: df                                                                                                                                                                                               
Out[2369]: 
   value  month
0   2345      1
1    123      2
2    282      9
3    367      1
4    213      2

In [2374]: import calendar                                                                                                                                                                                  

In [2375]: df['month_name'] = df['month'].apply(lambda x: calendar.month_abbr[x])                                                                                                                           

In [2384]: month_dict = df.groupby('month_name')['value'].apply(list).to_dict()                                                                                                                                          

In [2386]: for key, val in month_dict.items(): 
  ...:     print(key,val) 
  ...:                                                                                                                                                                                                  
Feb [123, 213]
Jan [2345, 367]
Sep [282]

推荐阅读