简体   繁体   English

如何基于与熊猫匹配的列值将数据从一个数据帧移动到另一个数据帧?

[英]How to move data from one dataframe to another based on matching column values with pandas?

I have two pandas DataFrames containing differently ordered data, and I am trying to move data from one to the other based on content. 我有两个pandas DataFrames,它们包含顺序不同的数据,并且我正在尝试根据内容将数据从一个移到另一个。 I've tried a lambda function, but I don't think I'm understanding how this is supposed to work. 我已经尝试过lambda函数,但是我认为我不了解这应该如何工作。 Here are simplified versions of my data: 这是我的数据的简化版本:

df1 = 
   Name  Pos  Opponent  DPAvPos
1  Dave  QB   DEN       NaN
2  Bill  QB   GB        NaN
3  Sean  QB   DET       NaN

df2 =
   Name  DKP/Game
1  DET   20.1
2  DEN   10.4
3  GB    15.2

I would like to move data from the DKP/Game column of df2 to the DPAvPos column df1 by matching data from the Opponent column of df1 with the Name column of df2. 我想通过将df1的Opponent列中的数据与df2的Name列进行匹配,将数据从df2的DKP / Game列移动到DPAvPos列df1。 So far everything I've tried hasn't worked. 到目前为止,我尝试过的所有方法均无效。

You could try to merge these two dataframes, then drop DPAvPos and rename DKP/Game as DPAvPos : 您可以尝试合并这两个数据帧,然后删除DPAvPos并将DKP / Game重命名为DPAvPos:

import pandas as pd

df1 = pd.DataFrame({
    "Name" : ["Dave", "Bill", "Sean"],
    "Pos" : ['QB', 'QB', 'QB'],
    "Opponent" : ["DEN", "GB", "DET"],
    "DPAvPos": ["NaN", "NaN", "NaN"]
})

df2 = pd.DataFrame({
    "Name" : ["DET", "DEN", "GB"],
    "DKP/Game" : [ 20.1, 23, 21]
})

df2.rename(columns={"Name" : "Opponent"}, inplace=True)
df3 = pd.merge(df1, df2, on="Opponent")
df3.drop('DPAvPos', axis=1, inplace=True)
df3.rename(columns={"DKP/Game" : "DPAvPos"}, inplace=True)

暂无
暂无

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

相关问题 熊猫:在一个数据框中创建新列,并根据与另一个数据框中的匹配键进行匹配 - Pandas: create new column in one dataframe with values based on matching key from another dataframe 基于匹配来自另一个数据帧pandas的值的新列 - New column based on matching values from another dataframe pandas Pandas:根据另一个 dataframe 的匹配条件移植列值(并以矢量化形式进行) - Pandas: Transplant column values from one dataframe based on matching condition of another (and do it in vectorized form) 根据 Pandas 中的列值将内容从一个 Dataframe 复制到另一个 Dataframe - Copy contents from one Dataframe to another based on column values in Pandas 如何在 Pandas 中将值从一个数据帧移动到另一个数据帧? - How to move values from one dataframe to another in pandas? pandas数据框根据另一数据框中的值将值追加到一列 - pandas dataframe append values to one column based on the values in another dataframe 如何在大熊猫不相等的值上将数据从一列移动到另一列? - How do i move data from one column to another on values that aren't equal in pandas? 将某些 pandas dataframe 列值从一列移到另一列,并将旧的 position 替换为 Nan - Move certain pandas dataframe column values from one column to another and replace old position with Nan 如何使用 pandas dataframe 将列添加到 dataframe 根据另一个 df 中的匹配列将数据标记为 1 或 0 - How to use pandas dataframe to add a column to a dataframe that labels data as 1 or 0 based on matching columns in another df 如何通过匹配来自另一个数据帧熊猫的值来填充数据帧中的列的值 - How to fill in values for a column in a dataframe by matching values from another dataframe pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM