简体   繁体   English

根据条件从当前行和上一行中的值创建列表

[英]Create list from value in current row and previous row based on condition

I have a dataframe with two columns 'a' and 'b' where 'b' is the difference between the value of 'a' and the previous value 'a'我有一个 dataframe 有两列'a'和'b',其中'b'是'a'的值和前一个值'a'之间的差异

df = pd.DataFrame({'a': [10, 60, 30, 80, 10]})
df['b'] = df['a']-df['a'].shift(1)

    a   b
0   10  NaN
1   60  50.0
2   30  -30.0
3   80  50.0
4   10  -70.0

I want to create a new column 'c' with values as a list of previous value of 'a' and the current value of 'a' (example, [60,30]) only where the column 'b' is negative.我想创建一个新列“c”,其值作为“a”的先前值和“a”的当前值(例如,[60,30])的列表,仅在“b”列为负的情况下。 Otherwise it has to be a list of the current value 'a' itself.否则,它必须是当前值“a”本身的列表。

The resulting output should look like生成的 output 应该看起来像

    a   b       c
0   10  NaN     [10]
1   60  50.0    [60]
2   30  -30.0   [60, 30]
3   80  50.0    [80]
4   10  -70.0   [80, 10]

Use list comprehension for create lists if b < 0 in numpy array with shifted helper column s by Series.shift added by DataFrame.assign :如果 numpy 数组中的b < 0使用列表推导创建列表,其中由Series.shift添加的DataFrame.assign移位辅助列s

arr = df.assign(s = df['a'].shift(fill_value=0))[['a','b','s']].to_numpy()
df['c'] = [[s,a] if b < 0 else [a] for  a,b,s in arr]
print (df)
    a     b             c
0  10   NaN        [10.0]
1  60  50.0        [60.0]
2  30 -30.0  [60.0, 30.0]
3  80  50.0        [80.0]
4  10 -70.0  [80.0, 10.0]  

Or is used Series.mask with one element list created by list comprenension:或者与由列表压缩创建的一个元素列表一起使用Series.mask

s = pd.Series([[x] for x in df['a']], index=df.index)
#alternative
s = df['a'].apply(lambda x: [x])

df['c'] = s.mask(df['b'].lt(0), s.shift() + s)
print (df)
    a     b         c
0  10   NaN      [10]
1  60  50.0      [60]
2  30 -30.0  [60, 30]
3  80  50.0      [80]
4  10 -70.0  [80, 10]

Use Series.to_numpy and increase the dimension by adding the newaxis then use boolean indexing with Series.lt and assign the new values:使用Series.to_numpy并通过添加 newaxis 来增加维度,然后使用 boolean 索引与Series.lt并分配新值:

df['c'] = df['a'].to_numpy()[:, None].tolist()
df.loc[df['b'].lt(0), 'c'] = df['c'].shift() + df['c']

Result:结果:

   a     b         c
0  10   NaN      [10]
1  60  50.0      [60]
2  30 -30.0  [60, 30]
3  80  50.0      [80]
4  10 -70.0  [80, 10]

Load the data:加载数据:

df = pd.DataFrame({'a': [10, 60, 30, 80, 10]})
df['b'] = df['a']-df['a'].shift(1)

Create a temporary Numpy matrix:创建一个临时的 Numpy 矩阵:

npa = np.array([df['a'].shift(1), df['a']]).transpose()

Write the matrix to a new df column 'c':将矩阵写入新的 df 列“c”:

df['c'] = list(npa)

Copy values in 'a' to 'c' if values in column 'b' are larger than 0 or NAN:如果“b”列中的值大于 0 或 NAN,则将“a”中的值复制到“c”:

df.loc[(df['b'] > 0) | (df['b'].isnull() == True) , 'c'] = pd.Series([[x] for x in df['a']])

暂无
暂无

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

相关问题 如何根据 pandas DataFrame 中的条件从当前行值中减去前一行值? - how to subtract previous row value from current row value based on condition in pandas DataFrame? 根据上一个行值创建一个新列并删除当前行 - Create a new column based on previous row value and delete the current row 基于前一行和当前行的多个 IF 条件 - Pandas - Multiple IF condition based on previous and the current row - Pandas 如何根据 python 中的条件将一行中的值替换为上一行? - How to replace a value in a row with the previous row based on a condition in python? 从Pandas列中的当前行值中减去前一行的值 - Subtract previous row value from the current row value in a Pandas column 如何将前一行中的值与当前行中的值相乘 - How to multiply value in previous row with a value from current row 根据“ID”列返回上一行值以查找当前行与上一行之间的差异 - Return previous row value based on "ID" column to find differences between current row and previous row 当对应的值为NaN时,如何根据另一列中的条件从前一行获取一个值? - How to get a value from a previous row when the corresponding value is NaN based on a condition in another column? 根据条件和前一行值从其他列填充 Pandas Dataframe 列 - Populate Pandas Dataframe column from other columns based on a condition and previous row value 将当前行值与前一行值进行比较 - Compare current row value to previous row values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM