访问同一DataFrame列中的先前值 [英] Access previous value in same DataFrame column

查看:103
本文介绍了访问同一DataFrame列中的先前值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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),但是如果以这种方式使用它,则会出现以下异常;

Which works fine but in the lambda, I want to access i['C'].shift(1) but I get following exception if use it this way;

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']是在此操作中添加的新列.

And also I want to avoid initialising a['C'] = 1, if it is possible, which means that a['C'] is a new column being added in this operation.

有什么建议或其他替代方法吗?

Any suggestions or alternate way of achieving this?

推荐答案

来自您的代码:

# Variable a BEFORE apply
   A   B
0  1   2
1  3   4
2  5   6
3  7   8
4  9  10

# Variable a AFTER apply
   A   B   C
0  1   2   4
1  3   4   8
2  5   6  12
3  7   8  16
4  9  10  20

假设此输出确实是您想要的,则:

Assuming this output is really what you want, then:

a = pd.DataFrame([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]], columns=['A', 'B'])

a['C'] = a['A'] + a['B'] + 1

我对为什么要访问a['C'].shift(1)感到有些困惑,因为无论如何所有值都是相同的,并且您试图不对其进行初始化.

I'm a little confused as to why you would want to access a['C'].shift(1) since all the values are the same anyway, and you are trying not to initialize it.

如果您想使用df.shift(n)的实际示例,请尝试:

If you want a working example of using df.shift(n), try:

a['Shift'] = a['A'] + a['B'].shift(1)

哪个会给你:

   A   B   C  Shift
0  1   2   4    NaN
1  3   4   8    5.0
2  5   6  12    9.0
3  7   8  16   13.0
4  9  10  20   17.0

这将为您提供A(i)+ B(i + 1),其中i是行号.由于您将B列移了1,所以第一个和是NaN.

This would give you A(i) + B(i+1), where i is the row number. Since you shifted column B by 1, the first sum is NaN.

这篇关于访问同一DataFrame列中的先前值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆