【问题标题】:Merge two data frame by comparing values but not the column name通过比较值而不是列名来合并两个数据框
【发布时间】:2020-12-22 01:25:23
【问题描述】:

DataFrame 1 - 按日期划分的水果价格(索引是日期

fruits_price = {'Apple': [9,5,14],
                'Orange': [10,12,10],
                'Kiwi': [5,4,20],
                'Watermelon': [4.4,5.4,6.4]}
df1 = pd.DataFrame(fruits_price,
                  columns = ['Apple','Orange','Kiwi','Watermelon'],
                  index=['2020-01-01','2020-01-02','2020-01-10'])

    date        Apple    Oranges  Kiwi   Watermelon ... Fruit_100
    2020-01-01   9        10       5     4.4
    2002-01-02   5        12       4     5.4
    ...
    2002-12-10   14       10       20    6.4  

Dataframe 2(按排名排列的顶级水果)(索引是日期

top_fruits = {'Fruit_1': ['Apple','Apple','Apple'],
              'Fruit_2': ['Kiwi','Orange','Kiwi'],
              'Fruit_3': ['Orange','Watermelon','Watermelon'],
              'Fruit_4': ['Watermelon','Kiwi','Orange']}
    
df2 = pd.DataFrame(top_fruits, 
                   columns = ['Fruit_1','Fruit_2','Fruit_3','Fruit_4'],
                   index=['2020-01-01','2020-01-02','2020-01-10'])

   date        Fruit_1  Fruit_2   Fruit_3        Fruit_4         ... Fruit_100
   2020-01-01   Apple   Kiwi      Oranges        Watermelon      Pineapple 
   2002-01-02   Apple   Oranges   Watermelon     Kiwi            Pineapple
   ...
   2002-12-10   Apple   Kiwi      Watermelon     Oranges         Pineapple

我想要 DataFrame 3(给定日期的顶级水果的价格) 它实际上告诉我在给定日期的顶级水果的价格

    date        Price_1    Price_2   Price_3     Price_4 ..... Price_100 
    2020-01-01   9        5          10           4.4
    2002-01-02   5        12         5.4          4
    ...
    2002-12-10   14       20         6.4          10

花了将近 1 晚,尝试迭代 Dataframe 2,然后在 DataFrame 1 上进行内部循环,并向 DataFrame 3 添加值。我已经尝试了几乎 6-7 种不同的方式,通过 iterrow ,iteritems,然后通过 iloc 直接将输出存储到df3。这些都不起作用。

只是想知道有一种更简单的方法可以做到这一点。 稍后我将乘以相同数据帧格式中的水果销售额。

【问题讨论】:

  • Does this help?lookup() 上的 W3 文章)。而不是self 使用其他df
  • 对不起,“使用其他数据框”是什么意思? @诺亚

标签: python pandas dataframe merge


【解决方案1】:

只要用apply函数,axis=1,就是一行一行,每一行是一个系列,名字就是日期,用df1中对应的行替换值。

df2.apply(lambda x: x.replace(df1.to_dict('index')[x.name]), axis=1)

【讨论】:

  • 不错的方法!如果df2 中的日期不在df1 中,它可能会返回KeyError。但是,使用df2[df2.index.isin(df1.index)] 可以轻松处理它。不错!
  • 优秀的方法
  • @dulq 成功了。我会尝试解开您的解决方案并学习它。
【解决方案2】:

通过df1创建dict,然后在df2上使用replace

import pandas as pd

fruits_price = {'Apple': [9,5,14],
            'Orange': [10,12,10],
            'Kiwi': [5,4,20],
            'Watermelon': [4.4,5.4,6.4]}
df1 = pd.DataFrame(fruits_price,
              columns = ['Apple','Orange','Kiwi','Watermelon'],
              index=['2020-01-01','2020-01-02','2020-01-10'])

top_fruits = {'Fruit_1': ['Apple','Apple','Apple'],
          'Fruit_2': ['Kiwi','Orange','Kiwi'],
          'Fruit_3': ['Orange','Watermelon','Watermelon'],
          'Fruit_4': ['Watermelon','Kiwi','Orange']}

df2 = pd.DataFrame(top_fruits, 
               columns = ['Fruit_1','Fruit_2','Fruit_3','Fruit_4'],
               index=['2020-01-01','2020-01-02','2020-01-10'])

result = df2.T.replace(df1.T.to_dict()).T
result.columns = [f"Price_{i}" for i in range(1, len(result.columns)+1)]
result

输出:

            Price_1 Price_2 Price_3 Price_4
2020-01-01  9.0     5.0     10.0    4.4
2020-01-02  5.0     12.0    5.4     4.0
2020-01-10  14.0    20.0    6.4     10.0

【讨论】:

    猜你喜欢
    • 2019-03-29
    • 2017-07-04
    • 2020-02-24
    • 2017-09-14
    • 1970-01-01
    • 2017-09-08
    • 2017-07-11
    • 1970-01-01
    • 2022-01-02
    相关资源
    最近更新 更多