简体   繁体   English

根据df1中的3个值与df2中的3个值匹配,在数据框中填充新列

[英]Filling new column in a dataframe based on 3 values in df1 matching 3 values in df2

Let's say I have 2 data frames, both share the columns " shoe ", " size ", and " color ". 假设我有2个数据框,它们都共享“ shoe ”,“ size ”和“ color ”列。 The smaller data frame contains each shoe model and has a column " price ". 较小的数据框包含每个鞋型号,并具有“ 价格 ”列。 The larger data frame contains every sold shoe but doesn't contain price (doesn't make sense I know but I'm just trying to explain my issue) 较大的数据框包含所有售出的鞋子,但不包含价格(我知道这没有道理,但我只是想解释一下我的问题)

I'd like to find a way to compile a new column for shoe price in the larger data frame, which will take the shoe price from the smaller data frame for every match of the 3 shared columns " shoe ", " size " and " color ". 我想找到一种在较大的数据框中编译鞋价格的新列的方法,对于3个共享列“ shoes ”,“ size ”和“ 颜色 ”。

I've tried merges which don't work due to the different sizes, I've tried using a for loop but honestly, I'm still a beginner. 我尝试过由于大小不同而无法使用的合并,我尝试过使用for循环,但老实说,我还是一个初学者。

Can anyone point me in the right direction? 谁能指出我正确的方向?

Here's some code to generate dataframes with random data: 以下是一些生成具有随机数据的数据帧的代码:

def Rand(start, end, num): 
res = [] 

for j in range(num): 
    res.append(random.randint(start, end)) 

return res


df1 = pd.DataFrame({"shoe":range(10), 
"size":range(1,11),"color":range(2,12),
'price':range(100,110)})

df2 = pd.DataFrame({"shoe": Rand(1, 10, 100),
"size": Rand(1, 11, 100), "color": Rand(1, 11, 100)})

Given the above dataframes, I'm trying to create a 'price' column in df2 that is found by matching df2's columns with matching values in the corresponding columns in df1 给定上述数据框,我试图在df2中创建一个“价格”列,该列是通过将df2的列与df1中相应列中的匹配值进行匹配而找到的

Maybe you need this: 也许您需要这样:

import pandas as pd
file1 = pd.DataFrame({"shoe":range(10), "size":range(1,11),"color":range(2,12), 'price':range(100,110)})
file2 = pd.DataFrame({"shoe":range(100), "size":range(1,101),"color":range(2,102)})
df = pd.merge(file1,file2,how='right', on = ["shoe","size","color"])
df.head()

In this example you will get new dataframe, which merge matching three columns. 在此示例中,您将获得新的数据框,该数据框合并匹配的三列。 You can read more here https://pandas.pydata.org/pandas-docs/stable/merging.html 您可以在这里阅读更多内容https://pandas.pydata.org/pandas-docs/stable/merging.html

暂无
暂无

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

相关问题 根据 df1 中的列值查找 df2 中的相交值,并使用 df1 中的检索值创建一个新列(不匹配的列名) - Lookup intersecting values in df2 based on column values in df1 and create a new column with retrieved value in df1 (non matching column names) 在DF2列值与DF1索引匹配的pandas DataFrame1中设置新的列值 - Set new column values in pandas DataFrame1 where DF2 column values match DF1 index 如何向 dataframe (df1) 添加一个新列,这是另一个 dataframe (df2) 中 df1 的多个查找值的总和 - How can I add a new column to a dataframe (df1) that is the sum of multiple lookup values from df1 in another dataframe (df2) Pandas 如何根据条件使用 DF2 中列的值在 DF1 中创建新列 - Pandas how to create new colum in DF1 with values of column in DF2 based on conditions 如何通过匹配 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 根据 df2 中的查询比较 df1 中的 2 个 dfs 和附加值 - Compare 2 dfs and append values in df1 based on query in df2 基于 df2 中的 .eq() 求和 df1 中的值 - Sum values in df1 based on .eq() in df2 从 DF2 替换 DF1 中的值 - Replace values in DF1 from DF2 pandas 如何从 df2 获取 df1 的值,而 df1 和 df2 的值在列上重叠 - pandas how to get values from df2 for df1 while df1 and df2 have values overlapped on column(s) 如何根据两个列值将我在 df1 中创建的唯一 ID 匹配到 df2? - How to match the unique ids that I created in df1 to df2 based on two column values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM