简体   繁体   English

DataFrame,如果特定列的值在DF1中,则将DF1中的值添加到DF2中的特定行中

[英]DataFrame, adding value from DF1 in specific row in DF2 if specific columns value is in DF1

I have been searching a lot on SO and in pandas's help but couldn't find what I am looking for. 我一直在SO和熊猫的帮助下进行大量搜索,但找不到我想要的东西。

I have 2 dataframes with those columns : 我有2个数据框与这些列:

Index([u'id', u'date', u'heure', u'titre'], dtype='object')

Index([u'article', u'id', u'type', u'rubrique', u'source', u'rebond_global',
   u'chargement_global', u'visites_global'],
  dtype='object')

What I'd love to be able to do is to keep the second one and to add data contained in the first dataframe using the 'id' as a key. 我希望能够做的是保留第二个数据,并使用“ id”作为键添加包含在第一个数据帧中的数据。

My final DataFrame always feels like I have made an append and added the new columns. 我最终的DataFrame总是让我感觉像是做了一个追加并添加了新列。

This is, amongst others, what I have tried : 这是我尝试过的其他方法:

Join method : 加盟方法:

df1.set_index('id').join(df2.set_index('id'))

Merge method : 合并方法:

pd.merge(df1, df2, how='outer', on='id')

In a way, what i'm trying to do is something similar to "if id from Dataframe 1 is in DataFrame 2 then create columns 'date', 'heure' and 'titre' in DataFrame 2 and fill with value from Dataframe 1" 在某种程度上,我正在尝试做的事情类似于“如果来自Dataframe 1的id在DataFrame 2中,然后在DataFrame 2中创建列'date','heure'和'titre'并填充Dataframe 1中的值”

Is there anyway to do this? 反正有这样做吗?

您要使用df2作为基础,然后使用“ id”列加入df1:

df2.join(df1.set_index('id'), 'id')

Try this: 尝试这个:

merged = pd.merge(left=df1[["id", "date", "heure", "titre"]], right=df2, on="id", how="inner")

Edit: Full example: 编辑:完整示例:

df1 = pd.DataFrame({
    "id": [1, 2, 3, 4],
    "date": [10, 20, 30, 40],
    "heure": ["h1", "h2", "h3", "h4"],
    "titre": ["t1", "t2", "t3", "t4"]
    })

df2 = pd.DataFrame({
    "id": [1, 2, 3, 5],
    "article": ["a1", "a2", "a3", "a5"]
    })

merged = pd.merge(left=df1[["id", "date", "heure", "titre"]], right=df2, on="id", how="inner")

print "DF1:\n", df1
print "DF2:\n", df2
print "Merged:\n", merged

Prints: 印刷品:

DF1:
   date heure  id titre
0    10    h1   1    t1
1    20    h2   2    t2
2    30    h3   3    t3
3    40    h4   4    t4
DF2:
  article  id
0      a1   1
1      a2   2
2      a3   3
3      a5   5
Merged:
   id  date heure titre article
0   1    10    h1    t1      a1
1   2    20    h2    t2      a2
2   3    30    h3    t3      a3

暂无
暂无

声明:本站的技术帖子网页,遵循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 PySpark:在 df1 中标记 df2 中存在的特定列的行? - PySpark: Flag rows of specific columns in df1, that exist in df2? 如果列值不在 df2 列中,则获取 df1 的行 - Get row of df1 if column value not in column df2 Python Pandas查找并替换df2中的df1值 - Python Pandas lookup and replace df1 value from df2 Go 遍历 dataframe 中的每一行,稍后搜索此值 dataframe,如果匹配,则从 df1 和另一个值 - Go through every row in a dataframe, search for this values in a second dataframe, if it matches, get a value from df1 and another value from df2 使用 df2 中的值,其中行值与 df1 列名匹配 - Use values from the df2 where row value matches df1 column name 如果df2索引中的df1索引,熊猫会更新列值 - Pandas update column value if df1 index in df2 index Pandas:如果df1列的值在df2列的列表中,则加入 - Pandas: Join if value of df1 column is in list of df2 column 如果 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 如果 df1 中的工作表名称与字符串值匹配,我如何将 df2 复制到 df1 - How can i copy a df2 to df1 if the name of the worksheet in df1 matches with a string value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM