python - 使用熊猫查找数据框列中的前 2 个
问题描述
未定义的行到列,按年份和列分组从此数据框中:
year month count reason
2001 1 1 a
2001 2 3 b
2001 3 4 c
2005 1 4 a
2005 1 3 c
使用此代码:
df2 = pd.pivot_table(df,index=["year","month",],values=["count"],columns="reason").reset_index().fillna(0)
df2.columns = [i[0] if i[0]!="count" else f"reason_{i[1]}" for i in df2.columns]
df2["count"] = df2.iloc[:,2:5].sum(axis=1)
print (df2)
改变数据框的结构变成这样:
year month reason_a reason_b reason_c count
2001 1 1 0 0 1
2001 2 0 3 0 3
2001 3 0 0 4 4
2005 1 4 0 3 7
然后,接下来我要选择列原因_?仅具有前 2 个较高值的:
find_top_two = [df2.iloc[:,2:-1].sum().nlargest(2)]
find_top_two
输出变成这样:
[reason_c 7.0
reason_a 5.0
dtype: float64]
但是,我想要的预期输出是数据框应该是这样的:
year month reason_a reason_c
2001 1 1 0
2001 2 0 0
2001 3 0 4
2005 1 4 3
谁能帮我解决这个问题?任何帮助,将不胜感激。先感谢您。
解决方案
多修改代码将获得您想要的输出
cols = ['year', 'month'] + df2.iloc[:,2:-1].sum().nlargest(2).index.tolist()
df2[cols]
Out[52]:
year month reason_c reason_a
0 2001 1 0 1
1 2001 2 0 0
2 2001 3 4 0
3 2005 1 3 4
推荐阅读
- ruby - rspec Object.new 未正确调用初始化
- html - 将 html 页面中的所有内容向下移动而不改变视频的位置
- node.js - 迁移到 CloudFlare 后,Chrome 中出现无效重定向错误
- netsuite - NetSuite 2015_2 PHP-SDK 两因素身份验证
- c++ - OpenCV 2.4.9 SimpleBlobDetector 调试断言失败..dbgdel.cpp line:52 _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
- daml - 如何将通过 DA Ledger API 接收的 DAML 元组转换为 Java 对?
- c# - 如何使用 dotnet-svcutil 生成的代码在 ASP.Net 内核中注入 WCF 服务客户端的单个实例?
- java - 如果我们有一块蛋糕,那么在数组中找到最高平均值的最有效算法是什么?
- swift - 将单例分配给变量(Swift)
- android - 带有改造的 Rxjava2 获取单个和所有数据