简体   繁体   中英

Iterating through one column and adding values to list based on value of another column in a PANDAS dataframe

I have a dataframe which follows this screenshot:

在此处输入图像描述

I would like to write a script which adds to a list, values from df['value'] . This list which the values are added to is dependant to the month number. So the intended output is:

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

The real df is a more complex but the problem is transferable. I have written this script with no luck:

    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)

Like this maybe:

Consider below sample dataframe:

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]

The name of the month has not been changed, but it is possible to summarize the month by the following measures.

d = df.groupby('month').agg(list)

this isn't the fastest nor the most efficient solution but based on what you tried, think this is what you're looking for.

jan = []
feb = []
mar = []
apr = []
may = []
jun = []
jul = []
aug = []
sep = []
oct = []
nov = []
dec = []


for i, row in stack.iterrows():
    val = row['value']
    month = row['month']

    if month == 1:
        jan.append(val)

    if month == 2:
        feb.append(val)

    if month == 3:
        mar.append(val)

    if month == 4:
        apr.append(val)

    if month == 5:
        may.append(val)

    if month == 6:
        jun.append(val)

    if month == 7:
        jul.append(val)

    if month == 8:
        aug.append(val)

    if month == 9:
        sep.append(val)

    if month == 10:
        oct.append(val)

    if month == 11:
        nov.append(val)

    if month == 12:
        dec.append(val)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM