我是靠谱客的博主 高贵野狼,最近开发中收集的这篇文章主要介绍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 – 在apply函数中使用shift()函数来比较Pandas Dataframe中的行所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部