计算后如何添加新列

时间:2018-07-29 21:32:11

标签: python pandas

我有这个DataFrame:

               Open     High      Low    Close AdjClose    Volume
datetime                                                         
2018-07-27  28.8200  29.3350  27.7050  28.1300  28.1300   8101362
2018-07-26  28.4800  29.1200  27.5500  28.9800  28.9800  10582061
2018-07-25  30.0900  30.0900  28.4800  28.9800  28.9800   9801897
2018-07-24  30.4500  30.9400  29.9650  30.1400  30.1400   5706941
2018-07-23  31.1100  31.3500  30.6000  30.8200  30.8200   6023310                  

我想在下面的for循环中计算两个新列(我使用的是python 2.7.15,而且我不是程序员)。 这是我的循环:

A=pd.DataFrame(df)
print (A.head(5))
Len=len(A)
print (Len)
for Rw in range(Len-1):
    def adj(A):
        BB=float(A.iloc[Rw,4])
        CC=float(A.iloc[Rw+1,4])
        #print (BB,CC)

        if BB>CC:
            x1=BB-CC
            x2=0   
            print(x1,x2)
        else:
            x1=0
            x2=CC-BB
            print(x1,x2)

        A.at[Rw+1,'Up']=x1   #Store calculation results in Up column
        A.at[Rw+1,'Down']=x2 #Store calculation results in Down column  

    adj(A)

print(A.head(5))

当我启用==> A.at [Rw + 1,'up'] = x1时,出现错误:

Traceback (most recent call last):
  File "D:\Yossi\eclipse-workspace_yossi\Tests\to_forum___003.py", line 85, in <module>
    adj(A)
  File "D:\Yossi\eclipse-workspace_yossi\Tests\to_forum___003.py", line 82, in adj
    A.at[Rw+1,'Up']=x1      #Store calculation results in UpMv columns
  File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 2159, in __setitem__
    self.obj._set_value(*key, takeable=self._takeable)
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 2587, in _set_value
    self.loc[index, col] = value
  File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 189, in __setitem__
    self._setitem_with_indexer(indexer, value)
  File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 375, in _setitem_with_indexer
    labels = index.insert(len(index), key)
  File "C:\Python27\lib\site-packages\pandas\core\indexes\datetimes.py", line 2194, in insert
    "cannot insert DatetimeIndex with incompatible label")
TypeError: cannot insert DatetimeIndex with incompatible label

如何使用DataFrame在“向上”列中查看x1的计算结果,并在“向下”列中查看x2的计算结果,如下图所示?

enter image description here

谢谢大家。

1 个答案:

答案 0 :(得分:1)

从定义循环中的函数开始,原始代码有很多错误。这是一个紧凑,高效,基于pandas / numpy的解决方案:

import numpy as np
difference = df['Close'] - df['Close'].shift()
df['Up'] = -np.minimum(difference, 0)
df['Down'] = np.maximum(difference, 0)