简体   繁体   English

用 Pandas 中的前一行列填充当前行和列值

[英]Fill current row and column value with previous row column in Pandas

I have set of readings and I need current and previous value to measure some parameters.我有一组读数,我需要当前值和先前值来测量某些参数。 How do I populate easily in pandas such that current row, column value takes from previous row, column value.如何在 pandas 中轻松填充,以便当前行、列值取自上一行、列值。 If prev row,col doesn't exit, then I want to fill with -1 or na or anything.如果 prev row,col 没有退出,那么我想用 -1 或na或任何东西填充。

 events  = np.array([[  3.,   -1., 441., 780.,  30., 262.,   0.,   0.,   0.,  -1.],
       [  4.,   3., 437., 770.,  30., 274.,   0.,   0.,   0.,  -1.],
       [  5.,   4, 431., 754.,  31., 286.,   0.,   0.,   0.,  -1.]]) 

df=pd.DataFrame(data=events, columns=["curr", "expected_prev", "f1", "f2", "f3", "f4", "f5", "f6", "f7","prev"])

For example, I want to fill values in panda dataframe as below.例如,我想在熊猫 dataframe 中填写值,如下所示。 The below expected_prev is hardcoded, how do I generate or fill the prev column in pandas.下面的expected_prev是硬编码的,我如何生成或填充 pandas 中的prev列。

在此处输入图像描述

Check with shift shift检查

df['exp_pre'] = df['curr'].shift().fillna(-1)

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

相关问题 从Pandas列中的当前行值中减去前一行的值 - Subtract previous row value from the current row value in a Pandas column Pandas 用行值填充列 - Pandas fill column with row value 根据条件创建一个 Boolean 列,该列涉及 Pandas 中当前行和上一行的列的值 - Create a Boolean column based on condition which involves value from a column of the current row and previous row in Pandas 如果列等于 Pandas 中的值,如何使当前行值等于前一行值? - How to make current row value equal to previous row value if a column equals a value in Pandas? Pandas - 添加一列,其值根据当前行和前一行中的另一列值计算得出 - Pandas - add a column with value computed based on another column value in current and previous row Pandas 列取决于其先前的值(行)? - Pandas column that depends on its previous value (row)? 根据上一个行值创建一个新列并删除当前行 - Create a new column based on previous row value and delete the current row 选择当前行中列值为 1 但上一行中值为 0 的行 - Selecting rows where column value is 1 in the current row, but 0 in the previous row Pandas 根据上一行不同的列值分配列值 - Pandas assign a column value basis previous row different column value Pandas 与当前行列匹配的先前记录的时间序列累积 - Pandas time series cumsum of previous records matching a column of current row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM