首页 > 解决方案 > 使用熊猫查找数据框列中的前 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

谁能帮我解决这个问题?任何帮助,将不胜感激。先感谢您。

标签: pythonpandasdataframe

解决方案


多修改代码将获得您想要的输出

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

推荐阅读