在列之间进行减法运算以在数据框中创建新列

时间:2019-10-13 17:12:01

标签: python dataframe

我当前的数据框:

             Adj Close    High  high_shift  high_>_high
Date                
2017-01-03   14.676315   15.65      14.70        True
2017-01-04   14.676315   15.68      15.65        True
2017-01-05   14.913031   15.91      15.68        True
2017-01-06   14.827814   15.92      15.91        True
2017-01-09   14.515349   15.60      15.92        False
2017-01-10   14.657379   15.68      15.60        True
2017-01-11   14.827814   15.68      15.68        False
2017-01-12   15.055059   16.25      15.68        True
2017-01-13   14.846750   15.95      16.25        False
2017-01-16   14.913031   15.75      15.95        False

如果高列的值大于high_shift列中的值,我想通过从列行adj close减去减去high_shift * 100的行值来创建一个新列列。

只是一个例子:

if (df.High > df.high_shift):
    df['new_column'] = (df['Adj Close'] - df['high_shift'])*100

如果高列的值不大于high_shift列的值,我希望新列中的值为0

我正在尝试以下代码行,但出现错误,甚至无法打印结果:

for i in df['high_>_high'], df['Close'], df['high_shift']:
    if df['high_>_high'][i]:
        (df['Close'][i] - df['high_shift'][i])*100

ValueError:系列的真值不明确。使用a.empty,a.bool(),a.item(),a.any()或a.all()。

我能够创建一列(高_> _高移位),显示何时高>  high_shift,但是我不能通过将此条件作为减去其他列的条件来创建新列

2 个答案:

答案 0 :(得分:0)

在处理Pandas中的数字数据时,usually最好避免使用Python循环(for / while)来支持Pandas的矢量化函数。

在这种情况下,可以使用Series.clip,它将给定边界之外的值带到边界。

df['new_column'] = ((df['Adj Close'] - df['high_shift']) * 100).clip(0)
# (.clip(0) could also go after the inner parentheses)

或者,您可以随后将列裁剪到位。

df['new_column'] = (df['Adj Close'] - df['high_shift']).clip(0) * 100
df['new_column'].clip(0, inplace=True)

对于比将值截断到范围更一般的情况,可以在系列(或DataFrame)上使用boolean indexing。 (如果您想了解更多。,该页面讨论了Pandas提供的许多索引编制。)

df['new'] = ((df['Adj Close'] - df['high_shift']) * 100).clip(0)
# Set all values in column 'new' below 0 to 0
df['new'][df['new'] < 0] = 0

答案 1 :(得分:0)

使用numpy.where

df['new_column'] = np.where(df.High > df.high_shift, (df.High - df.high_shift) * 100, 0)
print(df)

输出

         Date  Adj Close   High  high_shift  high_>_high  new_column
0  2017-01-03  14.676315  15.65       14.70         True        95.0
1  2017-01-04  14.676315  15.68       15.65         True         3.0
2  2017-01-05  14.913031  15.91       15.68         True        23.0
3  2017-01-06  14.827814  15.92       15.91         True         1.0
4  2017-01-09  14.515349  15.60       15.92        False         0.0
5  2017-01-10  14.657379  15.68       15.60         True         8.0
6  2017-01-11  14.827814  15.68       15.68        False         0.0
7  2017-01-12  15.055059  16.25       15.68         True        57.0
8  2017-01-13  14.846750  15.95       16.25        False         0.0
9  2017-01-16  14.913031  15.75       15.95        False         0.0