简体   繁体   English

Pandas df 获取上一行值

[英]Pandas df get previous row value

I have a pandas dataframe with null values:我有一个 pandas dataframe 和 null 值:

index指数 fecha费查 code代码 Place地方 dato1数据1 porcentaje_dato1 porcentaje_dato1 dato2数据2 dato3数据3 porcentaje_dato3 porcentaje_dato3
0 0 2021-01-04 2021-01-04 1 1个 Place1地点1 25809 25809 0.3 0.3 NaN钠盐 NaN钠盐 0.0 0.0
1 1个 2021-01-04 2021-01-04 2 2个 Place2地点2 2004 2004年 0.15 0.15 NaN钠盐 NaN钠盐 0.0 0.0
2 2个 2021-01-04 2021-01-04 3 3个 Place3地点3 9380 9380 0.92 0.92 NaN钠盐 NaN钠盐 0.0 0.0
3 3个 2021-01-04 2021-01-04 4 4个 Place4地点4 153 153 0.01 0.01 NaN钠盐 NaN钠盐 0.0 0.0
20 20 2021-01-05 2021-01-05 1 1个 Place1地点1 40263 40263 0.47 0.47 NaN钠盐 NaN钠盐 0.0 0.0
21 21 2021-01-05 2021-01-05 2 2个 Place2地点2 2985 2985 0.22 0.22 NaN钠盐 NaN钠盐 0.0 0.0
22 22 2021-01-05 2021-01-05 3 3个 Place3地点3 12929 12929 1.27 1.27 NaN钠盐 NaN钠盐 0.0 0.0
23 23 2021-01-05 2021-01-05 4 4个 Place4地点4 2656 2656 0.22 0.22 NaN钠盐 NaN钠盐 0.0 0.0
40 40 2021-01-07 2021-01-07 1 1个 Place1地点1 53934 53934 0.64 0.64 NaN钠盐 NaN钠盐 0.0 0.0
41 41 2021-01-07 2021-01-07 2 2个 Place2地点2 6186 6186 0.46 0.46 NaN钠盐 NaN钠盐 0.0 0.0
42 42 2021-01-07 2021-01-07 3 3个 Place3地点3 14406 14406 1.42 1.42 NaN钠盐 NaN钠盐 0.0 0.0
43 43 2021-01-07 2021-01-07 4 4个 Place4地点4 3190 3190 0.26 0.26 NaN钠盐 NaN钠盐 0.0 0.0
1415 1415 2021-04-14 2021-04-14 1 1个 Place1地点1 1970183 1970183 23.23 23.23 1419209.0 1419209.0 550974.0 550974.0 6.5 6.5
1416 1416 2021-04-14 2021-04-14 2 2个 Place2地点2 331419 331419 24.89 24.89 228547.0 228547.0 102872.0 102872.0 7.73 7.73
1417 1417 2021-04-14 2021-04-14 3 3个 Place3地点3 317019 317019 31.22 31.22 216006.0 216006.0 101013.0 101013.0 9.95 9.95
1418 1418 2021-04-14 2021-04-14 4 4个 Place4地点4 233042 233042 19.18 19.18 175460.0 175460.0 57582.0 57582.0 4.74 4.74
1436 1436 2021-04-15 2021-04-15 1 1个 Place1地点1 2041844 2041844 24.07 24.07 1481837.0 1481837.0 560007.0 560007.0 6.6 6.6
1437 1437 2021-04-15 2021-04-15 2 2个 Place2地点2 347963 347963 26.14 26.14 243497.0 243497.0 104466.0 104466.0 7.85 7.85
1438 1438 2021-04-15 2021-04-15 3 3个 Place3地点3 330038 330038 32.5 32.5 225213.0 225213.0 104825.0 104825.0 10.32 10.32
1439 1439 2021-04-15 2021-04-15 4 4个 Place4地点4 240488 240488 19.79 19.79 180775.0 180775.0 59713.0 59713.0 4.91 4.91

If value of dato2 is null, I need to fill it with dato1 value and sum previous day value for same place.如果 dato2 的值是 null,我需要用 dato1 的值填充它,并对同一位置的前一天值求和。 Steps to implement are实施步骤是

  • first order by place and date按地点和日期排序
  • iterate dataframe. For each row迭代 dataframe。对于每一行
    • Check if it is first row of entire df.检查它是否是整个 df 的第一行。 If so, dato2 = dato1如果是,则 dato2 = dato1
    • check if place has change (if place of actual row is different than place of previous row).检查位置是否有变化(如果实际行的位置与前一行的位置不同)。 Then dato2 = dato1那么dato2 = dato1
    • else: dato2 = dato2 previous row + dato1 actual row else: dato2 = dato2 上一行 + dato1 实际行

code I have is我的代码是

df = df.sort_values(by=['place', 'fecha']) 
for i, row in df.iterrows():
  if pd.isnull(row['dato2']):
    if i == 0:
      df['dato2'][i] = df['dato1'][i]
    elif df['place'][i] != df['place'][i-1]:
      df['dato2'][i] = df['dato1'][i]
    else:
      df['dato2'][i] = df['dato2'][i-1] + df_vac['dato1'][i]
  else:
    df['dato2'][i]

But with this code indexes are not valid.但是使用此代码索引无效。

Here's my approach.这是我的方法。

# Sort dataframe
df = (pd.read_csv(data)
        .sort_values(['Place','fecha']
        .reset_index())

# Fill missing values for dato2 with dato1
df['dato2'] = df.dato2.fillna(df.dato1)

# Calculate the aggregate, store in separate df
df_agg = (df[['Place','fecha','dato2']].groupby(['Place','fecha']).sum()
                                       .groupby('Place').cumsum()
                                       .reset_index())

# Update original data
df.update(df_agg)

Result:结果:

index指数 fecha费查 code代码 Place地方 dato1数据1 porcentaje_dato1 porcentaje_dato1 dato2数据2 dato3数据3 porcentaje_dato3 porcentaje_dato3
0 0 2021-01-04 2021-01-04 1 1个 Place1地点1 25809 25809 0.30 0.30 25809.0 25809.0 NaN钠盐 0.00 0.00
4 4个 2021-01-05 2021-01-05 1 1个 Place1地点1 40263 40263 0.47 0.47 66072.0 66072.0 NaN钠盐 0.00 0.00
8 8个 2021-01-07 2021-01-07 1 1个 Place1地点1 53934 53934 0.64 0.64 120006.0 120006.0 NaN钠盐 0.00 0.00
12 12 2021-04-14 2021-04-14 1 1个 Place1地点1 1970183 1970183 23.23 23.23 1539215.0 1539215.0 550974.0 550974.0 6.50 6.50
16 16 2021-04-15 2021-04-15 1 1个 Place1地点1 2041844 2041844 24.07 24.07 3021052.0 3021052.0 560007.0 560007.0 6.60 6.60
1 1个 2021-01-04 2021-01-04 2 2个 Place2地点2 2004 2004年 0.15 0.15 2004.0 2004.0 NaN钠盐 0.00 0.00
5 5个 2021-01-05 2021-01-05 2 2个 Place2地点2 2985 2985 0.22 0.22 4989.0 4989.0 NaN钠盐 0.00 0.00
9 9 2021-01-07 2021-01-07 2 2个 Place2地点2 6186 6186 0.46 0.46 11175.0 11175.0 NaN钠盐 0.00 0.00
13 13 2021-04-14 2021-04-14 2 2个 Place2地点2 331419 331419 24.89 24.89 239722.0 239722.0 102872.0 102872.0 7.73 7.73
17 17 2021-04-15 2021-04-15 2 2个 Place2地点2 347963 347963 26.14 26.14 483219.0 483219.0 104466.0 104466.0 7.85 7.85
2 2个 2021-01-04 2021-01-04 3 3个 Place3地点3 9380 9380 0.92 0.92 9380.0 9380.0 NaN钠盐 0.00 0.00
6 6个 2021-01-05 2021-01-05 3 3个 Place3地点3 12929 12929 1.27 1.27 22309.0 22309.0 NaN钠盐 0.00 0.00
10 10 2021-01-07 2021-01-07 3 3个 Place3地点3 14406 14406 1.42 1.42 36715.0 36715.0 NaN钠盐 0.00 0.00
14 14 2021-04-14 2021-04-14 3 3个 Place3地点3 317019 317019 31.22 31.22 252721.0 252721.0 101013.0 101013.0 9.95 9.95
18 18 2021-04-15 2021-04-15 3 3个 Place3地点3 330038 330038 32.50 32.50 477934.0 477934.0 104825.0 104825.0 10.32 10.32
3 3个 2021-01-04 2021-01-04 4 4个 Place4地点4 153 153 0.01 0.01 153.0 153.0 NaN钠盐 0.00 0.00
7 7 2021-01-05 2021-01-05 4 4个 Place4地点4 2656 2656 0.22 0.22 2809.0 2809.0 NaN钠盐 0.00 0.00
11 11 2021-01-07 2021-01-07 4 4个 Place4地点4 3190 3190 0.26 0.26 5999.0 5999.0 NaN钠盐 0.00 0.00
15 15 2021-04-14 2021-04-14 4 4个 Place4地点4 233042 233042 19.18 19.18 181459.0 181459.0 57582.0 57582.0 4.74 4.74
19 19 2021-04-15 2021-04-15 4 4个 Place4地点4 240488 240488 19.79 19.79 362234.0 362234.0 59713.0 59713.0 4.91 4.91

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM