【问题标题】:How to find the column sum by restricting the row size in dataframe?如何通过限制数据框中的行大小来查找列总和?
【发布时间】:2021-07-06 00:59:47
【问题描述】:

有一个数据框df1:

         DP 1     DP 2    DP 3   DP 4     DP 5    DP 6    DP 7   DP 8    DP 9    DP 10
OP 1    357848  1124788 1735330 2218270 2745596 3319994 3466336 3606286 3833515 3901463
OP 2    352118  1236139 2170033 3353322 3799067 4120063 4647867 4914039 5339085 
OP 3    290507  1292306 2218525 3235179 3985995 4132918 4628910 4909315     
OP 4    310608  1418858 2195047 3757447 4029929 4381982 4588268         
OP 5    443160  1136350 2128333 2897821 3402672 3873311             
OP 6    396132  1333217 2180715 2985752 3691712                 
OP 7    440832  1288463 2419861 3483130                     
OP 8    359480  1421128 2864498                         
OP 9    376686  1363294                             
OP 10   344014                                  

我想通过限制行数来计算每一列的总和。

To calculate sum of first column data, Sum(DP1) where row size should be 10-1

To calculate sum of second column data, Sum(DP2) where row size should be 10-2

To calculate sum of Third column data, Sum(DP3) where row size should be 10-3

等等..

输出是这样的:

    3327371  10251249  15047844  18447791  17963259  15954957  12743113  8520325  3833515

我尝试使用 for 循环:

>>dataframe_len = len(df1.columns)
>>print(dataframe_len)
   10
>>for i in range(0,10):
     #Here i need to find the sum of each column 
     #sum('col')(row size is 10-i)

这不是关于 DP1 到 DP10(10 列)有太多的列。

感谢您的宝贵时间 :)

【问题讨论】:

  • 10-1 是指10 to 1 还是10 minus 1?即,您是在尝试跳过顶行还是底行?根据您的预期输出,它看起来像是跳过了底行;或者更确切地说,跳过最后一个非 NA 值。

标签: python pandas dataframe triangle


【解决方案1】:

在这种情况下,您可以总结倒数第二个last_valid_index()

df.apply(lambda x: x.iloc[:df.index.get_loc(x.last_valid_index())].sum())

# DP 1      3327371.0
# DP 2     10251249.0
# DP 3     15047844.0
# DP 4     18447791.0
# DP 5     17963259.0
# DP 6     15954957.0
# DP 7     12743113.0
# DP 8      8520325.0
# DP 9      3833515.0
# DP 10           0.0

【讨论】:

    【解决方案2】:

    假设您希望它根据您的预期输出而不是根据您的描述,sum() 在删除 NA 值然后跳过最后一个值之后的每一列:

    df.apply(lambda col: col.dropna()[:-1].sum())
    

    输出:

    DP 1      3327371.0
    DP 2     10251249.0
    DP 3     15047844.0
    DP 4     18447791.0
    DP 5     17963259.0
    DP 6     15954957.0
    DP 7     12743113.0
    DP 8      8520325.0
    DP 9      3833515.0
    DP 10           0.0
    

    旁注:您的总和不是第 10-1、10-2、10-3 行等。它们是第 9-1、8-1、7-1 行。 IE。您正在跳过每列的 last 非 NA 值,而不是最上面的行。

    例如 df['DP 1'].sum()3671385 但跳过最后一行 df['DP 1'][:-1].sum()3327371 与您的预期输出匹配。对于 DP2:df['DP 2'].sum()11614543df['DP 2'].dropna()[:-1].sum()10251249(您的预期值)但 df['DP 2'][2:10].sum()9253616

    【讨论】:

    • 啊,更好,+1。我对shift(-1).last_valid_index() 过于复杂了
    • 我从你放的东西开始(但由于每列的移位量不同,所以无法让它工作)。然后意识到我们只是跳过了最后一个验证。
    【解决方案3】:

    我认为您可以在使用apply()时利用列名中的信息

    def sum_row(col):
        t = int(col.name.split(' ')[-1])
        return col.iloc[:-t].sum()
    
    df_ = df.apply(sum_row)
    
    # print(df_)
    
    DP 1      3327371.0
    DP 2     10251249.0
    DP 3     15047844.0
    DP 4     18447791.0
    DP 5     17963259.0
    DP 6     15954957.0
    DP 7     12743113.0
    DP 8      8520325.0
    DP 9      3833515.0
    DP 10           0.0
    dtype: float64
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 2018-03-18
      • 1970-01-01
      相关资源
      最近更新 更多