繁体   English   中英

在apply函数中使用shift()函数来比较Pandas Dataframe中的行

[英]Using a shift() function within an apply function to compare rows in a Pandas Dataframe

我想使用shift()从前一个索引中提取数据,前提是其中一个列Letter值是相同的。

import pandas as pd
df = pd.DataFrame(data=[['A', 'one'],
                        ['A', 'two'],
                        ['B', 'three'],
                        ['B', 'four'],
                        ['C', 'five']],
                  columns=['Letter', 'value'])

df['Previous Value'] = df.apply(lambda x : x['value'] if x['Letter'].shift(1) == x['Letter'] else "", axis=1)
print df

我收到错误:

AttributeError: ("'str' object has no attribute 'shift'", u'occurred at index 0')

期望的输出:

  Letter  value Previous Value
0      A    one               
1      A    two            one
2      B  three               
3      B   four          three
4      C   five               

使用where您的病情在当前行使用前一行匹配shift

In [11]:
df = pd.DataFrame(data=[['A', 'one'],
                        ['A', 'two'],
                        ['B', 'three'],
                        ['B', 'four'],
                        ['C', 'five']],
                  columns=['Letter', 'value'])
​
df['Previous Value'] = df['value'].shift().where(df['Letter'].shift() == df['Letter'], '')
df
​
Out[11]:
  Letter  value Previous Value
0      A    one               
1      A    two            one
2      B  three               
3      B   four          three
4      C   five               

您正在尝试将.shift()应用于给定行的给定列的值而不是Series。 我会这样做,使用groupby:

In [6]: df['Previous letter'] = df.groupby('Letter').value.shift()

In [7]: df
Out[7]:
  Letter  value Previous letter
0      A    one             NaN
1      A    two             one
2      B  three             NaN
3      B   four           three
4      C   five             NaN

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM