繁体   English   中英

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

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

假设我有2个数据框,它们都共享“ shoe ”,“ size ”和“ color ”列。 较小的数据框包含每个鞋型号,并具有“ 价格 ”列。 较大的数据框包含所有售出的鞋子,但不包含价格(我知道这没有道理,但我只是想解释一下我的问题)

我想找到一种在较大的数据框中编译鞋价格的新列的方法,对于3个共享列“ shoes ”,“ size ”和“ 颜色 ”。

我尝试过由于大小不同而无法使用的合并,我尝试过使用for循环,但老实说,我还是一个初学者。

谁能指出我正确的方向?

以下是一些生成具有随机数据的数据帧的代码:

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)})

给定上述数据框,我试图在df2中创建一个“价格”列,该列是通过将df2的列与df1中相应列中的匹配值进行匹配而找到的

也许您需要这样:

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()

在此示例中,您将获得新的数据框,该数据框合并匹配的三列。 您可以在这里阅读更多内容https://pandas.pydata.org/pandas-docs/stable/merging.html

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM