【问题标题】:Find the top 5 values based on the sum in the last column and last row根据最后一列和最后一行的总和查找前 5 个值
【发布时间】:2020-09-14 22:21:43
【问题描述】:

我想根据具有超过 20,000 行和 200 列的表集中的最后一列和最后一行的总和找到最高和最低 5 个值。 (这是一个多标签问题)。原始表没有列和行的总和。我自己添加了总和值)。在此处查看玩具数据集:

 import pandas as pd

 data = {'index': ['0001 ','0002 ','0003 ','0004 ','0005 ','0006 
    ','0007','0008','0009','0010','0011'],
    'factor1': [0,1,0,1,0,0,1,0,0,0,1],
    'factor2': [1,0,0,1,0,0,0,1,1,1,1], 
    'factor3': [1,1,1,1,0,0,0,1,1,0,1],
    'factor4': [0,1,1,1,0,0,1,1,0,0,1],
    'factor5': [1,1,1,1,0,0,0,1,1,1,1], 
    'factor6': [1,0,0,0,0,0,0,1,1,1,1],
    'factor7': [0,1,1,1,1,0,1,1,0,0,1],
    'factor8': [1,1,1,1,1,1,0,1,1,1,1], 
    'factor9': [1,0,0,0,0,0,0,0,0,0,0],
    }

    df = pd.DataFrame(data,columns=['index','factor1','factor2','factor3','factor4','factor5','factor6','factor7','factor8','factor9'])
    count_row = df.count(axis=1)
    df

这是生成的表格:

index   factor1 factor2 factor3 factor4 factor5 factor6 factor7 factor8 factor9
0   0001    0     1       1        0      1      1       0       1        1
1   0002    1     0       1        1      1      0       1       1        0
2   0003    0     0       1        1      1      0       1       1        0
3   0004    1     1       1        1      1      0       1       1        0
4   0005    0     0       0        0      0      0       1       1        0
5   0006    0     0       0        0      0      0       0       1        0 
6   0007    1     0       0        1      0      0       1       0        0
7   0008    0     1       1        1      1      1       1       1        0
8   0009    0     1       1        0      1      1       0       1        0
9   0010    0     1       0        0      1      1       0       1        0
10  0011    1     1       1        1      1      1       1       1        0

使用这段代码,我得到了每列和每行的总和

classSum=df.sum(axis=0) 
df["sum"] =df.sum(axis=1)
df =df.append(classSum,ignore_index=True)
rowSum=df.sum(axis=1)
df.at[11,'index']='Nan'
df

在列和行中有总和的表格:

    index   factor1 factor2 factor3 factor4 factor5 factor6 factor7 factor8 factor9 sum
  0  0001     0        1       1       0       1       1       0       1       1    6.0
  1  0002     1        0       1       1       1       0       1       1       0    6.0
  2  0003     0        0       1       1       1       0       1       1       0    5.0
  3  0004     1        1       1       1       1       0       1       1       0    7.0
  4  0005     0        0       0       0       0       0       1       1       0    2.0
  5  0006     0        0       0       0       0       0       0       1       0    1.0
  6  0007     1        0       0       1       0       0       1       0       0    3.0
  7  0008     0        1       1       1       1       1       1       1       0    7.0
  8  0009     0        1       1       0       1       1       0       1       0    5.0
  9  0010     0        1       0       0       1       1       0       1       0    4.0
  10 0011     1        1       1       1       1       1       1       1       0    8.0
  11 Nan      4        6       7       6       8       5       7       10      1    NaN

注意:第11行是总和行

我想要这样的结果:

基于行: - 前五个值的输出如下所示:

  factor 8 :10
  factor 5 : 8 
  factor 3 : 7
  factor 7 : 7
  factor 4 : 6

基于列:

-输出前 5 个值如下所示:

 0011 :8
 0008 :7
 0004 :7
 0001 :6
 0002 :6

总和中有相同的值。无视就好。

那我该怎么做呢?谢谢!

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    从您的原始数据开始,因此没有总和列,我们可以使用DataFrame.sum 来获得每列或每行的总和 (axis=1),然后我们将结果与 Series.nlargest 链接以获得前 5 名。

    df = df.set_index('index')
    

    前 5 列:

    df.sum().nlargest(5)
    
    factor8    10
    factor5     8
    factor3     7
    factor7     7
    factor2     6
    dtype: int64
    

    前 5 行:

    df.sum(axis=1).nlargest(5)
    
    index
    0011     8
    0004     7
    0008     7
    0001     6
    0002     6
    dtype: int64
    

    如果你真的想要字典,用to_dict链接解决方案:

    df.sum().nlargest(5).to_dict()
    
    {'factor8': 10, 'factor5': 8, 'factor3': 7, 'factor7': 7, 'factor2': 6}
    

    要绘制结果,请使用DataFrame.plot.bar

    df.sum().nlargest(5).plot.bar(figsize=(12,8))
    

    【讨论】:

      猜你喜欢
      • 2023-01-24
      • 2016-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 2021-11-23
      • 2014-07-12
      • 1970-01-01
      相关资源
      最近更新 更多