Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.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 熊猫如何让我的专栏在标价中附加新的价格,在持有或不做任何事情时保持相同的价格,全部出售为Nan=N_Python_Pandas - Fatal编程技术网

Python 熊猫如何让我的专栏在标价中附加新的价格,在持有或不做任何事情时保持相同的价格,全部出售为Nan=N

Python 熊猫如何让我的专栏在标价中附加新的价格,在持有或不做任何事情时保持相同的价格,全部出售为Nan=N,python,pandas,Python,Pandas,我有一个来自金价的数据框 Date Open High Low Close Long 20High LongPrice x/x/x 569.799988 575.299988 568.000000 572.500000 1 575.299988 NaN x/x/x 571.500000 574.200012 565.000000 567.400

我有一个来自金价的数据框

    Date      Open          High        Low         Close       Long  20High      LongPrice
    x/x/x     569.799988    575.299988  568.000000  572.500000  1     575.299988  NaN
    x/x/x     571.500000    574.200012  565.000000  567.400024  0     575.299988  NaN
    x/x/x     568.400024    574.000000  567.500000  570.200012  0     575.299988  NaN
    x/x/x     569.500000    571.000000  550.599976  551.000000  0     575.299988  NaN
    x/x/x     551.000000    553.299988  545.500000  550.099976  0     575.299988  NaN
    x/x/x     553.299988    566.000000  549.900024  564.500000  0     575.299988  NaN
    x/x/x     561.900024    561.900024  548.000000  550.200012  0     575.299988  NaN
    x/x/x     548.500000    549.500000  540.000000  539.000000  -1    575.299988  NaN
    x/x/x     538.000000    546.000000  535.500000  545.900024  -1    575.299988  NaN
    x/x/x     544.900024    545.000000  538.000000  539.700012  0     575.299988  NaN
我为他们做了这样的事情

  • Long=1-->买入
  • Long=0-->不做任何事情或保持不变
  • Long=-1-->全部卖出
事情会是这样的

  Date    Open          High        Low         Close       Long  20High      LongPrice
  x/x/x   569.799988    575.299988  568.000000  572.500000  1     575.299988  [575.299988]
  x/x/x   571.500000    575.299988  565.000000  567.400024  1     575.299988  [575.299988,575.299988]
  x/x/x   568.400024    574.000000  567.500000  570.200012  0     575.299988  [575.299988,575.299988]
  x/x/x   569.500000    571.000000  550.599976  551.000000  0     575.299988  [575.299988,575.299988]
  x/x/x   551.000000    553.299988  545.500000  550.099976  0     575.299988  [575.299988,575.299988]
  x/x/x   553.299988    566.000000  549.900024  564.500000  0     575.299988  [575.299988,575.299988]
  x/x/x   561.900024    561.900024  548.000000  550.200012  0     575.299988  [575.299988,575.299988]
  x/x/x   548.500000    549.500000  540.000000  539.000000  -1    575.299988  NaN
  x/x/x   538.000000    546.000000  535.500000  545.900024  -1    575.299988  NaN
  x/x/x   544.900024    577.000000  538.000000  560.700015  1     577.000000  [577.000000]
但我不确定我在代码中犯了什么错误,为什么不能使dataframe与我展示的示例中的dataframe类似(我也将使用LongPrice中的数据来计算利润)

def TurtleBuyPrice(df):
df=df.copy()
df=df.reset_index()
x=[]
对于索引,df.iterrows()中的行:
如果索引==0:
如果行['Long']==0或-1:
持续
其他:
df['LongPrice'][指数]=[行[“20High”]]
[1]中的elif行['Long']:
如果df['LongPrice'][index-1]==np.nan:
df['LongPrice'][指数]=[行[“20High”]]
其他:
df['LongPrice'][index]=df['LongPrice'][index-1]+[row[“20High”]]
[0]中的elif行['Long']:
df['LongPrice'][指数]=df['LongPrice'][指数-1]
[-1]中的elif行['Long']:
df['LongBuySell'][指数]=np.nan
返回df

如果有人有更好的方法来保存数据,请使用
shift
cumsum
给出一些建议:

s = df["Long"].ne(-1)
s2 = df["20High"].apply(lambda x: [x]) * df["Long"]
df["LongPrice"] = s2.groupby(s.ne(s.shift()).cumsum()).apply(pd.Series.cumsum)
print(df)
输出:

    Date        Open        High         Low       Close  Long      20High  \
0  x/x/x  569.799988  575.299988  568.000000  572.500000     1  575.299988   
1  x/x/x  571.500000  574.200012  565.000000  567.400024     1  575.299988   
2  x/x/x  568.400024  574.000000  567.500000  570.200012     0  575.299988   
3  x/x/x  569.500000  571.000000  550.599976  551.000000     0  575.299988   
4  x/x/x  551.000000  553.299988  545.500000  550.099976     0  575.299988   
5  x/x/x  553.299988  566.000000  549.900024  564.500000     0  575.299988   
6  x/x/x  561.900024  561.900024  548.000000  550.200012     0  575.299988   
7  x/x/x  548.500000  549.500000  540.000000  539.000000    -1  575.299988   
8  x/x/x  538.000000  546.000000  535.500000  545.900024    -1  575.299988   
9  x/x/x  544.900024  545.000000  538.000000  539.700012     1  575.299988   

                  LongPrice  
0              [575.299988]  
1  [575.299988, 575.299988]  
2  [575.299988, 575.299988]  
3  [575.299988, 575.299988]  
4  [575.299988, 575.299988]  
5  [575.299988, 575.299988]  
6  [575.299988, 575.299988]  
7                        []  
8                        []  
9              [575.299988]  

我不知道如何通过计算来获得
LongPrice
我只是为了节省我在计算利润之前购买的1个单位,如果系统需要出售我以前没有使用ne和cumsum这样的函数,所以我没有想到这个想法,但它是否有更多的选择不使用cumsum?到目前为止,除了逐行循环外,我想不出其他不使用
cumsum
的方法,这会失去
pandas
的许多优点。哦,好吧,那么我也会将其用于计算我的总利润。顺便说一句,谢谢