这是我的代码:
a = pd.DataFrame([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]], columns=['A', 'B'])
print(a)
a['C'] = 1 # or np.nan or is there a way to avoid this?
b = lambda i : i['A'] + i['B'] + i['C'] # actually what is needed if to access a previous element, like i['C'].shift()
a['C'] = a.apply(b, axis=1)
print(a)
效果很好,但在 lambda 中,我想访问i['C'].shift(1)
但如果这样使用它,我会得到以下异常;
Traceback (most recent call last):
File "C:\Users\Development\workspace\TestPython\TestPython.py", line 31, in <module>
a['C'] = a.apply(b, axis=1)
File "C:\Program Files\Python36\lib\site-packages\pandas\core\frame.py", line 4262, in apply
ignore_failures=ignore_failures)
File "C:\Program Files\Python36\lib\site-packages\pandas\core\frame.py", line 4358, in _apply_standard
results[i] = func(v)
File "C:\Users\Development\workspace\TestPython\TestPython.py", line 29, in <lambda>
b = lambda i : i['A'] + i['B'] + i['C'].shift() # actually what is needed if to access a previous element, like i['C'].shift()
AttributeError: ("'numpy.int64' object has no attribute 'shift'", 'occurred at index 0')
而且我想避免初始化a['C'] = 1
,如果可能的话,这意味着 a['C'] 是在此操作中添加的新列。
有什么建议或替代方法来实现这一目标吗?