简体   繁体   English

根据 Pandas 数据框中的事件填充当前行中的下一行事件

[英]Populate next row event in current row based on the event in Pandas dataframe

BrkPressState BrkPressState VehSpdGS车速
1 1 2 2
1 1 3 3
1 1 2 2
1 1 4 4
0 0 12 12
0 0 13 13
0 0 11 11
1 1 3 3
0 0 15 15
0 0 14 14
0 0 15 15
1 1 12 12
1 1 13 13
0 0 14 14

For the above table i am trying to populate the next row value in previous last event, Like the below table对于上表,我试图在上一个事件中填充下一行值,如下表所示

I tried with Shift - 1 but its populating only for the current row , Sample code which i tried.我尝试使用 Shift - 1 但它仅填充当前行,我尝试过的示例代码。

d['result']=d.loc[d['BrkPressState'] != d['BrkPressState'].shift(-1), 'VehSpdGS'] 

Expected output:预期输出:

在此处输入图像描述

You can use two masks for finding vals and idxs and set values to index for result column.您可以使用两个掩码来查找valsidxs ,并将值设置为result列的索引。

mask1 = df['BrkPressState'] != df['BrkPressState'].shift()
vals = df.loc[mask1, 'VehSpdGS'][1:].values

mask2 = df['BrkPressState'] != df['BrkPressState'].shift(-1)
idxs = df.loc[mask2, 'VehSpdGS'][:-1].index

df.loc[idxs, 'result'] = vals
print(df)

    BrkPressState  VehSpdGS  result
0               1         2     NaN
1               1         3     NaN
2               1         2     NaN
3               1         4    12.0
4               0        12     NaN
5               0        13     NaN
6               0        11     3.0
7               1         3    15.0
8               0        15     NaN
9               0        14     NaN
10              0        15    12.0
11              1        12     NaN
12              1        13    14.0
13              0        14     NaN

Let us do diff to compare the previous and current row in BrkPressState column in order to identify boundaries, then mask and shift the values in VehSpdGS column让我们做diff来比较BrkPressState列中的前一行和当前行以识别边界,然后maskshift VehSpdGS列中的值

m = df['BrkPressState'].diff().ne(0)
df['Results'] = df['VehSpdGS'].mask(~m).shift(-1)

    BrkPressState  VehSpdGS  Results
0               1         2      NaN
1               1         3      NaN
2               1         2      NaN
3               1         4     12.0
4               0        12      NaN
5               0        13      NaN
6               0        11      3.0
7               1         3     15.0
8               0        15      NaN
9               0        14      NaN
10              0        15     12.0
11              1        12      NaN
12              1        13     14.0
13              0        14      NaN

You can also do shift(-1) on VehSpdGS and then replace values with NaN if df['BrkPressState'] != df['BrkPressState'].shift(-1)如果df['BrkPressState'] != df['BrkPressState'].shift(-1)你也可以在VehSpdGS上做shift(-1)然后用NaN替换值

Code:代码:

df["result"]=df["VehSpdGS"].shift(-1).where(df['BrkPressState'] != df['BrkPressState'].shift(-1),pd.NA)
df

output:输出:

BrkPressState BrkPressState VehSpdGS车速 result结果
0 0 1 1 2 2 NaN
1 1 1 1 3 3 NaN
2 2 1 1 2 2 NaN
3 3 1 1 4 4 12.0 12.0
4 4 0 0 12 12 NaN
5 5 0 0 13 13 NaN
6 6 0 0 11 11 3.0 3.0
7 7 1 1 3 3 15.0 15.0
8 8 0 0 15 15 NaN
9 9 0 0 14 14 NaN
10 10 0 0 15 15 12.0 12.0
11 11 1 1 12 12 NaN
12 12 1 1 13 13 14.0 14.0
13 13 0 0 14 14 NaN

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

相关问题 使用 Pandas 将 DataFrame 中的当前行与下一行进行比较 - Compare current row with next row in a DataFrame with pandas 下一行与当前行的区别 pandas dataframe - Difference between the next row and the current row pandas dataframe pandas.DataFrame.rolling 根据 Id 和时间索引将当前行值和下 2 行值相加,到第 1 行的值 - pandas.DataFrame.rolling summing the current row value and the next 2 row value based on Id and time index, to the 1st row's value Python,Pandas和for循环:根据与列表值的匹配填充数据框行 - Python, Pandas and for loop: Populate dataframe row based on a match with list values 根据上一个填充当前行 - Populate the current row based on the prev 在数据框熊猫中将行与下一行合并 - merge row with next row in dataframe pandas 熊猫通过基于当前行的值过滤DataFrame添加列 - pandas adding a column via filtering the DataFrame based on values of current row 根据每行上一行中的值更新pandas dataframe当前行属性 - Update pandas dataframe current row attribute based on its value in the previous row for each row 如何根据具有连续相同值的列事件删除数据框中的行 - How to delete a row in a dataframe based on a column event with consecutive same value 熊猫根据当前行的值选择行 - Pandas select row based on value of current row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM