[英]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.