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

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

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

以下是您想要的内容:

  1. import pandas as pd
  2. df1 = pd.DataFrame({
  3. 'ID': [1, 2, 3],
  4. 'name': [
  5. "阅读文档",
  6. "在提问之前检查是否已有相同问题",
  7. "创建一个MRE"
  8. ]
  9. })
  10. df2 = pd.DataFrame({
  11. 'ID': [1],
  12. 'url': ["http://pandas.pydata.org/docs/"]
  13. })
  14. df3 = pd.DataFrame({
  15. 'ID': [2],
  16. 'url': ["https://stackoverflow.com/search"]
  17. })
  18. 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')
  19. # 输出:
  20. ID name new_col
  21. 0 1 阅读文档 A
  22. 1 2 在提问之前检查是否已有相同问题 B
  23. 2 3 创建一个MRE I
英文:

Here is what it appears you want:

  1. import pandas as pd
  2. df1 = pd.DataFrame({
  3. 'ID': [1, 2, 3],
  4. 'name': [
  5. "read the documentation",
  6. "check your question hasn't been asked before",
  7. "create an MRE"
  8. ]
  9. })
  10. df2 = pd.DataFrame({
  11. 'ID': [1],
  12. 'url': ["http://pandas.pydata.org/docs/"]
  13. })
  14. df3 = pd.DataFrame({
  15. 'ID': [2],
  16. 'url': ["https://stackoverflow.com/search"]
  17. })
  18. 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')
  19. # Output:
  20. ID name new_col
  21. 0 1 read the documentation A
  22. 1 2 check your question hasn't been asked before B
  23. 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:

确定