python - 遍历一列并根据 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)
解决方案
可能像这样:
考虑下面的示例数据框:
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]
推荐阅读
- python - 如何使用 Python 确保 JSON 中的匹配引号?
- android - 有没有办法在 Android 上识别其他人的应用图像?
- javascript - 将数据从 React 表单发送到 Nodemailer
- bootstrap-4 - 如何在某些元素溢出卡或偏离正常流程的情况下创建 Bootstrap 4 卡
- pyqt - PyQt 中的 Windows 资源管理器导航栏
- c# - 离开 WebView 后播放 YouTube 视频
- javascript - 尝试对单个路径使用多个组件
- c - 动态结构的C动态数组
- python - 用于开发环境的 Docker 设置?
- c# - 如何在 c# 中正确连接和查询到 SQL Server 异步