如何在计算后添加新列

2024-04-20 04:57:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个数据帧:

               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循环中计算两个新列(我使用的是python2.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))

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

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

如何使用数据帧查看“Up”列中x1的计算结果和“Down”列中x2的计算结果,如下图所示?你知道吗

enter image description here

谢谢大家。你知道吗


Tags: inpypandaslibpackageslinesitefile
1条回答
网友
1楼 · 发布于 2024-04-20 04:57:13

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

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

相关问题 更多 >