对于循环,请遍历列中每个项目的ID

时间:2019-03-31 14:33:56

标签: python

我想遍历每个id和每个水果的数据框,以便每个水果都有与该id和两个价格相关的其他水果。

我尝试使用itertuples,但是元组是新的列名,价格是值:

for line in df4.itertuples():
    df4[line[1]-1, line[2]-1] = line[3]
df4

example and expected results

1 个答案:

答案 0 :(得分:0)

使用pandas Dataframe时总是尝试避免for循环,它具有更有效的功能。

这是使用pandas.merge的一种方法:

import pandas as pd

if __name__ == '__main__':
    df = pd.DataFrame({
        'id': [1, 1, 1, 2, 2, 2],
        'fruit': ['apple', 'grape', 'pear', 'grape', 'apple', 'pear'],
        'price': [3, 4, 2, 2, 5, 1]
    }).set_index('id')
    print(pd.merge(left=df, right=df, how='inner', left_index=True, right_index=True)\
          [lambda df: df.fruit_x < df.fruit_y])

则输出为:

   fruit_x  price_x fruit_y  price_y
id                                  
1    apple        3   grape        4
1    apple        3    pear        2
1    grape        4    pear        2
2    grape        2    pear        1
2    apple        5   grape        2
2    apple        5    pear        1

此输出与屏幕快照中显示的输出不同,但是与您的问题描述匹配:它产生具有相同ID的所有不同水果对。 另外请注意,此代码假定没有多个水果出现相同ID的情况。

相关问题