【问题标题】:get corresponding column value from another dataframe pandas从另一个数据框 pandas 获取相应的列值
【发布时间】:2018-10-23 07:13:27
【问题描述】:

以下是我正在比较的两个数据框。当我能够匹配列Item 时,我想在df2 的列Usage 下获得相应的列值。感谢帮助。

df1 = pd.DataFrame({ 'Number':[1.0,3.0,4.0,5.0,8.0,12.0,32.0,58.0,72.0] , 'Item': ['Phone', 'Watch', 'Pen', 'Pencil', 'Pencil', 'toolkit', 'box', 'fork', 'toy']})
df2 = pd.DataFrame({'Number':[3.0, 4.0, 8.0, 12.0, 15.0, 32.0, 54.0, 58.0, 72.0], 'Item':['Watch', 'Pen', 'Pencil', 'Eraser', 'bottle', 'box', 'toolkit', 'fork', 'Phone'], 'Usage':['Time', 'Writing', 'Writing', 'Cleaning', 'Water', 'storage', 'Utility', 'Eat', 'Communication']})

df1
   Number     Item
0     1.0    Phone
1     3.0    Watch 
2     4.0      Pen
3     5.0   Pencil
4     8.0   Pencil   
5    12.0  toolkit
6    32.0      box
7    58.0     fork
8    72.0      toy

df2
   Number     Item          Usage
0     3.0    Watch           Time
1     4.0      Pen        Writing
2     8.0   Pencil        Writing
3    12.0   Eraser       Cleaning
4    15.0   bottle          Water
5    32.0      box        storage
6    54.0  toolkit        Utility
7    58.0     fork            Eat
8    72.0    Phone  Communication

用于匹配的代码如下。即使只有数字匹配,它也会显示“MatchedBoth”。这需要修复。

import numpy as np
df3 = df1.copy()
df3['Matching'] = np.nan
df3.loc[(df3.Number.isin(df2.Number)) & (df3.Item.isin(df2.Item)), 'Matching'] = 'MatchedBoth'
df3.loc[(df3.Number.isin(df2.Number)) & (~df3.Item.isin(df2.Item)),'Matching'] = 'Matched Number Only'
df3.Matching.fillna('No Match', inplace=True)

在同一代码中是否有可能嵌入一个返回值,该返回值可以从 df2 获取 Usage 列值,对应于每个匹配的行。可能存在多行可以匹配的情况,因此我们可能需要将相应的Usage 列值放入列表或最终输出中的类似内容中。

注意:在我的实际数据框中,除了这些之外,我还有几列,因此如果我使用合并,它会导致一个巨大的数据框。我只想创建一个新列,其中包含在 df2 的 Usage 列中找到的相应匹配值列表。

输出应如下所示。

df3
   Number     Item             Matching    Usage
0     1.0    Phone             No Match      NaN
1     3.0    Watch          MatchedBoth     Time
2     4.0      Pen          MatchedBoth  Writing
3     5.0   Pencil             No Match      NaN
4     8.0   Pencil          MatchedBoth  Writing 
5    12.0  toolkit  Matched Number Only  Utility
6    32.0      box          MatchedBoth  storage
7    58.0     fork          MatchedBoth      Eat
8    72.0      toy  Matched Number Only     Play

【问题讨论】:

    标签: pandas compare match multiple-columns


    【解决方案1】:

    你可以试试这样的:

    df3 = df1.merge(df2, on='Number', how='left')
    df3['Matching'] = np.where(df3.Productdetailed == df3.Item, 'Matched', 'No Match')
    df3.drop('Productdetailed', axis=1, inplace=True)
    

    这将返回您在问题中指定的输出。

    澄清后编辑:

    def find_match(row):
      if (row.Number in df2.Number.values) & (row.Item in df2.Item.values):
          return "MatchedBoth"
      elif ((row.Number in df2.Number.values) & ~(row.Item in df2.Item.values)):
          return "Matched Number Only"
      else:
          return "No Match"
    
    df3['Matching'] = df3.apply(find_match, axis=1)
    df3['Usage'] = df3.Item.map(df2.set_index('Item').Usage)
    

    【讨论】:

    • 感谢您的回答。但在我的代码中,我正在进行多级比较。我想比较 Item 和 Number 列,并在 Matching 列中告诉 MatchedBoth。如果只有数字匹配,那么我想说 MatchedNumberOnly。同时获取Usage列对应的值。涉及多个匹配标准。此外,在我的原始数据集中,如果我合并,我会得到多个其他不需要的列,从而使结果数据框非常庞大。
    • 抱歉,您的示例有点不清楚,您创建了df3 = df1.copy() and then use conditionals like (df3.Item.isin(df2.Item)` 但df3 没有Item 列。是Productdetailed df1 中的列假设命名为 Item?
    • 抱歉打错了。我现在已经编辑过了。你是对的。我将 Item 和 Number 列与两个条件进行比较。但是我需要的是,当我比较并且我知道列匹配时,我需要使用相应的状态更新一个名为 Matching 的新列,并且另一列应该收集使用列中的一个或多个值并将其放入新列中df3 数据框。在我的实际数据集中,我有多个列,因此合并选项可能会使事情复杂化。我的问题是我没有在问题中提到这一点。我现在要编辑问题
    • 请告知是否可以使用 .map() 或 .apply() 之类的东西。
    • 感谢您的耐心等待。这有帮助!。
    猜你喜欢
    • 2023-02-13
    • 1970-01-01
    • 2020-09-06
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 2021-04-28
    相关资源
    最近更新 更多