3 回答
TA贡献2019条经验 获得超9个赞
您正在寻找两个数据帧的笛卡尔积。在 Pandas 中解决此问题的一种方法是为两个数据帧创建一个通用且唯一的键并执行merge(任何,因为存在完全重叠):
df.assign(key=0).merge(object_raw.assign(key=0), on='key').drop(['key'], axis=1)
brand_name category_id object_name
0 Nike 24 T-shirt
1 Nike 45 Shorts
2 Nike 32 Dress
3 Lacoste 24 T-shirt
4 Lacoste 45 Shorts
5 Lacoste 32 Dress
6 Adidas 24 T-shirt
7 Adidas 45 Shorts
8 Adidas 32 Dress
TA贡献1752条经验 获得超4个赞
另一种使用itertools.product()它的方法给出输入迭代的笛卡尔积。
import itertools
df=(pd.DataFrame(list(itertools.product(brand_name.brand_name,object_raw.object_name))
,columns=['brand_name','object_name']))
df['category_id']=df['object_name'].map(object_raw.set_index('object_name')['category_id'])
print(df)
brand_name object_name category_id
0 Nike T-shirt 24
1 Nike Shorts 45
2 Nike Dress 32
3 Lacoste T-shirt 24
4 Lacoste Shorts 45
5 Lacoste Dress 32
6 Adidas T-shirt 24
7 Adidas Shorts 45
8 Adidas Dress 32
TA贡献1842条经验 获得超21个赞
这被称为笛卡尔积。在熊猫中它有点棘手,但你可以这样做:
import pandas as pd
objects = pd.DataFrame(["T-shirt","Shorts","Dress"],columns = ['object'])
brands = pd.DataFrame(["Nike","Lacoste","Adidas"],columns = ['brand'])
objects["key"] = 1
brands ["key"] = 1
objects.merge(brands,on='key').drop('key',axis=1)
添加回答
举报