简体   繁体   English

遍历一列并根据 PANDAS dataframe 中另一列的值将值添加到列表

[英]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:我有一个 dataframe 如下截图:

在此处输入图像描述

I would like to write a script which adds to a list, values from df['value'] .我想编写一个脚本,将df['value']添加到列表中。 This list which the values are added to is dependant to the month number.添加值的列表取决于月份数。 So the intended output is:所以预期的 output 是:

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.真正的df更复杂,但问题是可以转移的。 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:考虑下面的示例 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pandas数据框根据另一数据框中的值将值追加到一列 - pandas dataframe append values to one column based on the values in another dataframe Select 列动态在 Pandas dataframe 基于列表或另一列中的值 - Select column dynamically in Pandas dataframe based on values in a list or another column 基于另一列的值列表的一列中的 Pandas 查找值 - Pandas lookup values in one column based on list of values of another column 添加基于 pandas 中另一列值的列列表 - Adding a column list based on another column values in pandas 获取基于另一列的列值,其中包含pandas dataframe中的字符串列表 - get column value based on another column with list of strings in pandas dataframe Pandas Dataframe:根据将一列的每个值与另一列的所有值进行比较来分配新列 - Pandas Dataframe: assigning a new column based comparing each value of one column to all the values of another Pandas DataFrame:为什么我不能通过行迭代基于另一列的值来更改一列的值? - Pandas DataFrame: Why I can't change the value of one column based on value of another through row iteration? 根据另一个中的值将值添加到pandas数据帧的一列中 - Add values to one column of a pandas dataframe based on the values in another 根据另一列的值将列添加到pandas数据框中 - Adding columns to a pandas dataframe based on values of another column 根据来自另一个 dataframe pandas 的值添加列 - Adding a column based on the values from another dataframe pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM