Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有一种方法可以使用布尔函数在熊猫系列中实现动作函数?_Python_Python 3.x_Pandas_List_Conditional Statements - Fatal编程技术网

Python 有没有一种方法可以使用布尔函数在熊猫系列中实现动作函数?

Python 有没有一种方法可以使用布尔函数在熊猫系列中实现动作函数?,python,python-3.x,pandas,list,conditional-statements,Python,Python 3.x,Pandas,List,Conditional Statements,我试图实现股票市场收盘价的动作函数,逻辑是 Action [ i ] = 1 if Close[ i+1 ] > Close[ i ] Action [ i ] = (-1) if Close[ i+1 ] < Close[ i ] 我希望在一个新的dataframe操作中使用输出 1 if df['Adj close'][0] < df['Adj Close'][1] -1 if df['Adj close'][0] > df['Adj Close'][1] Adj

我试图实现股票市场收盘价的动作函数,逻辑是

Action [ i ] = 1 if Close[ i+1 ] > Close[ i ]
Action [ i ] = (-1) if Close[ i+1 ] < Close[ i ]
我希望在一个新的dataframe操作中使用输出

1 if df['Adj close'][0] < df['Adj Close'][1]
-1 if df['Adj close'][0] > df['Adj Close'][1]

Adj Close   Action
1144.973755  1
1133.032471 -1
1130.913696 -1
1133.176880  1
1135.295532  1
1127.495117 -1

1如果df['Adj close'][0]df['Adj close'][1]
闭合动作
一千一百四十四点九七三七五五一
1133.032471 -1
1130.913696 -1
一千一百三十三点一七六八八零一
一千一百三十五点二九五五三二一
1127.495117 -1
如果我想实现此功能,我应该如何编写条件语句?

通过以下方式将ed值与原始值进行比较并设置新值:

详细信息

print (df['Adj Close'].shift())
0            NaN
1    1144.973755
2    1133.032471
3    1130.913696
4    1133.176880
5    1135.295532
Name: Adj Close, dtype: float64

print (df['Adj Close'].shift() > df['Adj Close'])
0    False
1     True
2     True
3    False
4    False
5     True
Name: Adj Close, dtype: bool
自定义功能:

def action(df):
    df['Action'] = np.where(df['Adj Close'].shift() > df['Adj Close'], -1, 1)
    return df
print (action(df))
     Adj Close  Action
0  1144.973755       1
1  1133.032471      -1
2  1130.913696      -1
3  1133.176880       1
4  1135.295532       1
5  1127.495117      -1
第一个值与零比较,所以如果使用
diff
函数,则输出缺少值。如果需要自定义值,这里有另一个解决方案:

或:


或者np.选择当diff=0时是否执行特殊操作

预期输出列是什么?@jezrael i编辑了该问题以回答您的问题,价格没有变化的情况如何?我们可以定义一个函数来编写相同的操作吗?
print (df['Adj Close'].shift())
0            NaN
1    1144.973755
2    1133.032471
3    1130.913696
4    1133.176880
5    1135.295532
Name: Adj Close, dtype: float64

print (df['Adj Close'].shift() > df['Adj Close'])
0    False
1     True
2     True
3    False
4    False
5     True
Name: Adj Close, dtype: bool
def action(df):
    df['Action'] = np.where(df['Adj Close'].shift() > df['Adj Close'], -1, 1)
    return df
print (action(df))
     Adj Close  Action
0  1144.973755       1
1  1133.032471      -1
2  1130.913696      -1
3  1133.176880       1
4  1135.295532       1
5  1127.495117      -1
diff = df['Adj Close'].diff()
df['Action'] = np.select([diff.isna(), diff > 0], [0, 1], -1)
shifted = df['Adj Close'].shift()
df['Action'] = np.select([shifted > df['Adj Close'], shifted < df['Adj Close']], [-1, 1], 0)

print (df)
     Adj Close  Action
0  1144.973755       0
1  1133.032471      -1
2  1130.913696      -1
3  1133.176880       1
4  1135.295532       1
5  1127.495117      -1
df['Action'] = np.where(df['Adj Close'].shift(-1) > df['Adj Close'], 1, -1)
print (df)
     Adj Close  Action
0  1144.973755      -1
1  1133.032471      -1
2  1130.913696       1
3  1133.176880       1
4  1135.295532      -1
5  1127.495117      -1
df['Action'] = np.where(df['Adj Close'].diff()>0, 1,-1)