比较Pandas

时间:2018-06-08 20:12:01

标签: python pandas dataframe

我有一个股票数据框:

   Open     High      Low    Close  Volume        rb          us  \
0  1.20821  1.20821  1.20793  1.20794  138.96  0.022347  100.000000   
1  1.20794  1.20795  1.20787  1.20788  119.61  0.004967   85.714286   
2  1.20788  1.20793  1.20770  1.20779  210.42  0.007451   64.285714   
3  1.20779  1.20791  1.20779  1.20789   77.51  0.008280   83.333333   
4  1.20789  1.20795  1.20789  1.20792   56.97  0.002484   50.000000   

           ls  color  
0   96.428571  black  
1   85.714286  black  
2   50.000000  black  
3  100.000000  white  
4  100.000000  white  

我想将当前行数据与之前的行数据进行比较,如下所示:

if(df['color'] == df['color'].shift(-1)):
   if((df['Close'] >= df['Open'].shift(-1) and df['Open']>=df['Close'].shift(-1)):
      df['Position'] = UP
   if((df['Close'] < df['Open'].shift(-1) and df['Open']<=df['Close'].shift(-1)):
      df['Position'] = DOWN

如果有条件,还有很多......

无法通过

比较数据
np.where(condition,TRUE,FALSE)

由于我的算法中有很多条件。这只是其中的一部分。

我该如何检查这些条件?

2 个答案:

答案 0 :(得分:3)

你需要:

df['Position'] = np.where((df['Volume'] > df['Volume'].shift(-1)) & ((df['Close'] >= df['Close'].shift(-1)) & (df['Open'] <= df['Open'])),"UP","DOWN")

输出:

        Open    High    Low Close   Volume  Position
0   1.20821 1.20821 1.20793 1.20794 138.96  UP
1   1.20794 1.20795 1.20787 1.20788 119.61  DOWN
2   1.20788 1.20793 1.20770 1.20779 210.42  DOWN
3   1.20779 1.20791 1.20779 1.20789 77.51   DOWN
4   1.20789 1.20795 1.20789 1.20792 56.97   DOWN

答案 1 :(得分:1)

我会在不同的检查中划分它:

diffdf = df.diff()[1:]

cond1 = diffdf['Volume'] > 0
cond2 = diffdf['Close'] >= 0
# cond3 = diffdf['Open'] >= 0 ?? What is this supposed to check?

df['Position'] = np.insert(np.where(cond1&cond2, 'UP', 'DOWN'), 0, '-')

df.diff()

      Open     High      Low    Close  Volume
0      NaN      NaN      NaN      NaN     NaN
1 -0.00027 -0.00026 -0.00006 -0.00006  -19.35
2 -0.00006 -0.00002 -0.00017 -0.00009   90.81
3 -0.00009 -0.00002  0.00009  0.00010 -132.91
4  0.00010  0.00004  0.00010  0.00003  -20.54

完整示例:

import pandas as pd
import numpy as np

data = '''\
Open     High      Low    Close  Volume
1.20821  1.20821  1.20793  1.20794  138.96
1.20794  1.20795  1.20787  1.20788  119.61
1.20788  1.20793  1.20770  1.20779  210.42
1.20779  1.20791  1.20779  1.20789   77.51
1.20789  1.20795  1.20789  1.20792   56.97'''

fileobj = pd.compat.StringIO(data)
df = pd.read_csv(fileobj, sep='\s+')

# Our client wants to know what stocks have increased in Volumne and Close
# We call this element Position and it is either Up,Down,-
# Let us create a difference dataframe and check those conditions
diffdf = df.diff()[1:]
cond1 = diffdf['Volume'] > 0
cond2 = diffdf['Close'] >= 0
df['Position'] = np.insert(np.where(cond1&cond2, 'UP', 'DOWN'), 0, '-')

print(df)

返回:

      Open     High      Low    Close  Volume Position
0  1.20821  1.20821  1.20793  1.20794  138.96        -
1  1.20794  1.20795  1.20787  1.20788  119.61     DOWN
2  1.20788  1.20793  1.20770  1.20779  210.42     DOWN
3  1.20779  1.20791  1.20779  1.20789   77.51     DOWN
4  1.20789  1.20795  1.20789  1.20792   56.97     DOWN