简体   繁体   English

如何用来自另一个 dataframe (df2) 的信息填充 dataframe (df1) 的列? 就在 df1 和 df2 中的两列信息匹配时?

[英]How to fill a column of a dataframe (df1) with info from another dataframe (df2)? Just when two column info matches in df1 and df2?

I have a df1 with 10k rows like:我有一个 df1 有 10k 行,例如:

name  time  day  details  year

xxx    1    mon   AA

yyy    2    tue   BB

zzz    3    mon   CC

And i have a df2 with 2k rows like:我有一个 df2 有 2k 行,例如:

  time   details  year
   4      AA       1900

   2      BB       2000

   5      CC       2030

When time and details of row of df1 is equal to time and detals of the row in df2, i want get the year info of df2 and update df1.当 df1 行的时间和详细信息等于 df2 中行的时间和详细信息时,我想获取 df2 的年份信息并更新 df1。 The desire df is like that:欲望df是这样的:

name  time  day  details  year
    
xxx    1    mon   AA

yyy    2    tue   BB   2000

zzz    3    mon   CC

Try a 'left' merge on 'time' and 'details':在“时间”和“细节”上尝试“左” merge

import numpy as np
import pandas as pd

df1 = pd.DataFrame({
    'name': ['xxx', 'yyy', 'zzz'],
    'time': [1, 2, 3],
    'day': ['mon', 'tue', 'mon'],
    'details': ['AA', 'BB', 'CC'],
    'year': [np.nan, np.nan, np.nan]
})

df2 = pd.DataFrame({
    'time': [4, 2, 5],
    'details': ['AA', 'BB', 'CC'],
    'year': [1900, 2000, 2030]
})

merged = df1.drop(columns='year').merge(df2, on=['time', 'details'], how='left')

print(merged)

merged : merged

  name  time  day details    year
0  xxx     1  mon      AA     NaN
1  yyy     2  tue      BB  2000.0
2  zzz     3  mon      CC     NaN

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 df1 中的键列与 df2 中的多个列匹配时,使用另一个数据框 (df2) 列中的值更新数据框 (df1) 列 - Update a dataframe(df1) column with value from another dataframe(df2) column when a key column in df1 matches to multiple columns in df2 如果 df1 column1 中的值与列表中的值匹配,Pandas 从另一个 df1 column2 在 df2 中创建新列 - Pandas create new column in df2 from another df1 column2 if a value in df1 column1 matches value in a list pandas 如何从 df2 获取 df1 的值,而 df1 和 df2 的值在列上重叠 - pandas how to get values from df2 for df1 while df1 and df2 have values overlapped on column(s) 如果 df2 中不存在列,如何将列从 df1 添加到 df2,否则什么也不做 - How to add a column from df1 to df2 if it not present in df2, else do nothing 如何通过匹配 df1 中与 df2 索引和列名匹配的列值来用 df1 中的数据填充 df2 - How to fill df2 with data from df1 by matching column values from df1 which match df2 index and column names 一列 (df1) 与两列 (df2) 与 python 进行比较 - One column (df1) compare with two column(df2) with python 如何将两个不同的数据框 df1 df2 与特定列(列 w)进行比较,并从 df2 更新 df1 中匹配的行列 AD - how to compare two different data frames df1 df2 with specific column ( column w) and update the matched rows column AD in df1 from df2 如何在熊猫中进行“(df1&not df2)”数据框合并? - How to do "(df1 & not df2)" dataframe merge in pandas? 使用包含 2 个数据框和日期范围的 IP 列来使用来自 df2 的数据填充 df1 数据框 - Use IP column of 2 dataframes and date range to populate df1 dataframe with data from df2 熊猫:如何正确执行df2中的行= df1中的列? - Pandas: how to do row in df2 = column in df1 properly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM