与熊猫分组比较值,多行

时间:2021-01-23 04:42:26

标签: python pandas dataframe group-by

pic1

我有一个数据框,它在每两行的“代码”列中具有相同的值。

我想在最右侧创建一个列名 ['test'] 比较每个 groupby ['code'] 中 "V" 的值,并在 ['test'] 的列中返回 true 或 false

例如

我想将 0050 的第一个“代码”中的“V”(30367315) 中的值与“V”(14029981) 的值与“0050”的第二个值进行比较

0050 如果 30367315 > 14029981,['test'] 返回真。

0051 如果 56966 > 46687,['test'] 返回真。

0052 如果 2447344 > 2798834,['test'] 返回 false。

这是尝试过的:

container=[]
for _df in td2.groupby(['code']):
    _df = pd.DataFrame(_df)
    num1 = _df.iloc[1,0]
    num2 = _df.iloc[1,1]
    _df['test'] = num2 > num1
    container.append(_df)
    ch_ = pd.concat(container)

这是预期的结果: pic2

今天,我的结果返回为

IndexError: single positional indexer is out-of-bounds

但前几天我尝试了这段代码,它奏效了。 我不确定我做了什么。我的 jupyter 笔记本已自动保存,我必须在某处触摸代码 我不明白为什么。

0 个答案:

没有答案