我是靠谱客的博主 高贵野狼,这篇文章主要介绍python中shift_python – 在apply函数中使用shift()函数来比较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

最后

以上就是高贵野狼最近收集整理的关于python中shift_python – 在apply函数中使用shift()函数来比较Pandas Dataframe中的行的全部内容,更多相关python中shift_python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(106)

评论列表共有 0 条评论

立即
投稿
返回
顶部