如何在dataframe中进行复杂计算

pgccezyw  于 2021-09-29  发布在  Java
关注(0)|答案(2)|浏览(215)

示例 Dataframe :

df = pd.DataFrame({'sales': ['2020-01','2020-02','2020-03','2020-04','2020-05','2020-06'],
                   '2020-01': [24,42,18,68,24,30],
                   '2020-02': [24,42,18,68,24,30],
                   '2020-03': [64,24,70,70,88,57],
                   '2020-04': [22,11,44,3,5,78],
                   '2020-05': [11,35,74,12,69,51]}

我想在下面找到 df['L2'] 我研究了Pandas滚球,群比,等等,都不能解决它。
请阅读l2公式并给我一个意见
l2公式

L2(Jan-20) = 24
-------------------
     sales  2020-01
0  2020-01       24
-------------------

L2(Feb-20) = 132  (sum of below matrix 2x2)

     sales  2020-01  2020-02
0  2020-01       24       24
1  2020-02       42       42
-------------------
L2(Mar-20) = 154 (sum of matrix 2x2)

     sales  2020-02  2020-03
0  2020-02       42       24
1  2020-03       18       70
-------------------
L2(Apr-20) = 187 (sum of below maxtrix 2x2)

     sales  2020-03  2020-04
0  2020-03       70       44
1  2020-04       70        3

输出

Unnamed: 0   sales  Jan-20  Feb-20  Mar-20  Apr-20  May-20   L2   L3
0           0  Jan-20      24      24      64      22      11   24   24
1           1  Feb-20      42      42      24      11      35  132  132
2           2  Mar-20      18      18      70      44      74  154  326
3           3  Apr-20      68      68      70       3      12  187  350
4           4  May-20      24      24      88       5      69   89  545
5           5  Jun-20      30      30      57      78      51  203  433
nwwlzxa7

nwwlzxa71#

Values=f.values[:,1:]
L2=[]
RANGE=Values.shape[0]
for a in range(RANGE):
    if a==0:
        result=Values[a,a]
    else:
        if Values[a-1:a+1,a-1:a+1].shape==(2,1):
            result=np.sum(Values[a-1:a+1,a-2:a])

        else:
            result=np.sum(Values[a-1:a+1,a-1:a+1])

    L2.append(result)
print(L2)
L2 output:-->[24, 132, 154, 187, 89, 203]

f["L2"]=L2

f:

oaxa6hgo

oaxa6hgo2#

import pandas as pd
import numpy as np

# make a dataset

df = pd.DataFrame({'sales': ['2020-01','2020-02','2020-03','2020-04','2020-05','2020-06'],
                   '2020-01': [24,42,18,68,24,30],
                   '2020-02': [24,42,18,68,24,30],
                   '2020-03': [64,24,70,70,88,57],
                   '2020-04': [22,11,44,3,5,78],
                   '2020-05': [11,35,74,12,69,51]})

print(df)

# datawork(L2)

for i in range(0,df.shape[0]):

    if i==0:
        df.loc[i,'L2']=df.loc[i,'2020-01']

    else:
        if i!=df.shape[0]-1:
            df.loc[i,'L2']=df.iloc[i-1:i+1,i:i+2].sum().sum()

        if i==df.shape[0]-1:
             df.loc[i,'L2']=df.iloc[i-1:i+1,i-1:i+1].sum().sum()

print(df)

# sales  2020-01  2020-02  2020-03  2020-04  2020-05     L2

# 0  2020-01       24       24       64       22       11   24.0

# 1  2020-02       42       42       24       11       35  132.0

# 2  2020-03       18       18       70       44       74  154.0

# 3  2020-04       68       68       70        3       12  187.0

# 4  2020-05       24       24       88        5       69   89.0

# 5  2020-06       30       30       57       78       51  203.0

相关问题