[英]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.