假设我有2个数据框,其中包括餐厅和类别。 我试图创建一个新列,其中将包含同一区域中的餐厅数量以及共有至少一个类别的餐厅。
我该如何处理?这是我到目前为止所做的
Restaurant contains: id, zone
id Zone ...
0 11 H5X ...
1 12 H2A
2 13 H5X
3 14 H53
4 15 H21 ...
Category contains: id, category
id category ...
0 11 Sushi ...
1 12 Fast Food
2 13 Sandwich
3 13 Sushi
4 14 Noodle
5 14 Fast Food
6 15 Bakeries ...
现在如何创建与originalDF的新列“ intersection”?结果如下:
id Zone intersection
0 11 H5X 1 (since there is one restaurant, id=13, that is in the same zone(H5X
and have at least one category in common, Sushi)
1 12 H2A 0
3 13 H5X 1 (since there is one restaurant, id =11, that is in the same zone (h5x) andat
least one category in common , sushi)
5 14 H53 0
6 15 H21 0
有人可以帮助我吗,我迷路了。谢谢
答案 0 :(得分:2)
import pandas as pd
# create both datasets
df1 = pd.DataFrame({
'id': [11, 12, 13, 14, 15],
'zone': ['H5X', 'H2A', 'H5X', 'H53', 'H21']
})
df1.head()
df2 = pd.DataFrame({
'id': [11, 12, 13, 13, 14, 14, 15],
'category': ['Sushi', 'Fast food', 'Sandwich', 'Sushi', 'Noodle', 'Fats food', 'Bakeries']
})
df2.head()
# merge datasets based on restaurant id
df3 = pd.merge(df1, df2, how='left', on=['id'])
df3.reset_index(drop=True, inplace=True)
df3.head()
输出:
# count repeating zone / category
cnt = df3.groupby(['zone', 'category']).size().to_frame('count')
cnt.head(10)
输出:
# merge counts to first dataframe to achieve desired result
df4 = pd.merge(df1, cnt, how='left', on='zone')
df4['count'] = df4['count'].apply(lambda x: 0 if x <=1 else 1)
df4.rename(columns={'count': 'intersection'}, inplace=True)
df4.head()
输出: