检查3个不同数据框中的3列,并创建一个新列。

huangapple go评论69阅读模式
英文:

Check 3 columns in 3 different dataframes and create a new column

问题

我有三个类似这样的数据框:

ID Name
First row
Second row

所有的数据框都有一个共同的ID列。有一个主数据框,其他两个需要与之进行比较。
我想要实现的逻辑是:
1)如果第二个数据框的ID与第一个匹配,则创建一个列并给它一个常量'A'的值。
2)如果第三个数据框的ID与第一个匹配,在相同的列中给它一个常量'B'的值。
3)如果它在其中一个中都没有出现,在相同的列中给它一个常量'I'的值。

我正在尝试使其更高效,而不是创建两个for循环。任何帮助将不胜感激!

英文:

I have three dataframes like this

ID Name
First row
Second row

All the dataframe have ID column in common. There is a main dataframe against which the other two needs to be compared.
Logic I want to implement:

  1. if ID's of second dataframe matches with the first, then create a column and give it a constant 'A' value.
  2. if ID's of third dataframe matches with the first, in the same column give it a constant 'B' value.
  3. if it doesn't appear in either of them,in the same column give it a constant 'I'

I am trying to make it efficient instead of creating two for loops. Any help will be appreciated!

答案1

得分: 1

以下是您想要的内容:

import pandas as pd

df1 = pd.DataFrame({
    'ID': [1, 2, 3],
    'name': [
        "阅读文档",
        "在提问之前检查是否已有相同问题",
        "创建一个MRE"
    ]
})

df2 = pd.DataFrame({
    'ID': [1],
    'url': ["http://pandas.pydata.org/docs/"]
})

df3 = pd.DataFrame({
    'ID': [2],
    'url': ["https://stackoverflow.com/search"]
})

df1['new_col'] = df1['ID'].apply(lambda x: 'A' if x in df2['ID'].values else 'B' if x in df3['ID'].values else 'I')

# 输出:
   ID                              name new_col
0   1                            阅读文档       A
1   2  在提问之前检查是否已有相同问题               B
2   3                            创建一个MRE       I
英文:

Here is what it appears you want:

import pandas as pd

df1 = pd.DataFrame({
    'ID': [1, 2, 3],
    'name': [
        "read the documentation",
        "check your question hasn't been asked before",
        "create an MRE"
    ]
})

df2 = pd.DataFrame({
    'ID': [1],
    'url': ["http://pandas.pydata.org/docs/"]
})

df3 = pd.DataFrame({
    'ID': [2],
    'url': ["https://stackoverflow.com/search"]
})

df1['new_col'] = df1['ID'].apply(lambda x: 'A' if x in df2['ID'].values else 'B' if x in df3['ID'].values else 'I')

# Output:
   ID                                          name new_col
0   1                        read the documentation       A
1   2  check your question hasn't been asked before       B
2   3                                 create an MRE       I

huangapple
  • 本文由 发表于 2023年7月28日 05:57:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定