[英]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.