![](/img/trans.png)
[英]How to efficiently interpolate data in a Pandas DataFrame row-wise?
[英]Pandas DataFrame: efficiently factorize row-wise set difference
我正在寻找一种更 Pythonic/更有效(和更短)的方法来分解(=枚举唯一实例)聚合 dataframe 中的逐行集差异。 下表说明了 dataframe 操作不应过于复杂:
产品 | 产品组 | 地点 |
---|---|---|
10 | 1 | 排 |
11 | 1 | 我们 |
12 | 1 | 加州 |
13 | 2 | 排 |
14 | 2 | J.P |
15 | 2 | 我们 |
16 | 3 | FR |
17 | 3 | 是 |
18 | 4 | 排 |
19 | 4 | 我们 |
20 | 4 | 加州 |
应该使用list_locations = ['US', 'CA', 'JP', 'BE', 'FR']
转换成表格的表格
产品组 | 位置列表 | rest_of_world_location_list | rest_of_world_index |
---|---|---|---|
1 | 行,美国,加利福尼亚 | 日本、比利时、法国 | 第 1 行 |
2 | 行,JP,美国 | 加利福尼亚、比利时、法国 | 第 2 行 |
4 | 行,美国,加利福尼亚 | 加利福尼亚、比利时、法国 | 第 1 行 |
这样每个product group
都有一个列rest_of_world_location_list
列出了list_locations
中不属于产品组的所有项目。 列rest_of_world_index
只是rest_of_world_location_list
的分解。
MWE输入数据:
df = pd.DataFrame(
{
"product": [10,11,12,13,14,15,16,17,18,19,20],
"product_group": [1,1,1,2,2,2,3,3,4,4,4],
"location": ['RoW', 'US', 'CA', 'RoW', 'JP', 'US', 'FR', 'BE', 'RoW', 'US', 'CA']
}
)
list_locations = ['US', 'CA', 'JP', 'BE', 'FR']
我的尝试(有效,但可能太复杂了):
activity_with_rest_of_world_location = pd.DataFrame(df[df['location'] == 'RoW']['product_group'])
activity_with_rest_of_world_location['index'] = activity_with_rest_of_world_location.index
df_rest_of_world = df[df['product_group'].isin(activity_with_rest_of_world_location['product_group'])]
df_rest_of_world = df_rest_of_world.drop(activity_with_rest_of_world_location['index'])
df_rest_of_world_agg = pd.DataFrame(
data = df_rest_of_world.groupby('product_group')['location'].apply(tuple))
df_rest_of_world_agg.reset_index(inplace = True)
df_rest_of_world_agg = df_rest_of_world_agg.merge(
right = activity_with_rest_of_world_location,
how = 'left',
on = 'product_group'
)
df_rest_of_world_agg.set_index(keys = 'index', inplace = True)
df_rest_of_world_agg['location_rest_of_world'] = df_rest_of_world_agg.apply(
lambda row: tuple(set(list_io_countries) - set(row['location'])),
axis = 1
)
df_rest_of_world_agg = df_rest_of_world_agg.dropna(subset ='location_rest_of_world')
df_rest_of_world_agg['location'] = pd.factorize(df_rest_of_world_agg['location_rest_of_world'])[0]
df_rest_of_world_agg['location'] = 'RoW_' + df_rest_of_world_agg['location'].astype(str)
IIUC,您可以通过 3 个步骤使用单个管道:
world = set(list_locations)
(df.groupby('product_group', as_index=False)
# aggregate locations as string and the rest of the from from a set difference
.agg(**{'location_list': ('location', ', '.join),
'rest_of_world_location_list': ('location', lambda l: ', '.join(sorted(world.difference(l))))
})
# filter the rows without RoW
.loc[lambda d: d['location_list'].str.contains('RoW')]
# add category
.assign(rest_of_world_index=lambda d: 'RoW_'+d['location_list'].astype('category').cat.codes.add(1).astype(str)
)
)
output:
product_group location_list rest_of_world_location_list rest_of_world_index
0 1 RoW, US, CA BE, FR, JP RoW_2
1 2 RoW, JP, US BE, CA, FR RoW_1
3 4 RoW, US, CA BE, FR, JP RoW_2
具有set
s 的解决方案 - 每个组的 crete 集,过滤掉没有RoW
行,通过join
获得差异,最后使用frozenset
s 进行factorize
:
list_io_countries = ['US', 'CA', 'JP', 'BE', 'FR']
s = set(list_io_countries)
df = df.groupby(df['product_group'])['location'].agg(set).reset_index(name='location_list')
df = (df[['RoW' in x for x in df['location_list']]]
.assign(rest_of_world_location_list = lambda x: x['location_list'].apply(lambda x: ','.join(s - x)),
rest_of_world_index = lambda x: pd.factorize(x['location_list'].apply(lambda x: frozenset(x - set(['RoW']))))[0] + 1,
location_list = lambda x: x['location_list'].agg(','.join)
)
.assign(rest_of_world_index = lambda x: 'RoW_' + x['rest_of_world_index'].astype(str)))
print (df)
product_group location_list rest_of_world_location_list rest_of_world_index
0 1 RoW,CA,US JP,BE,FR RoW_1
1 2 RoW,JP,US CA,BE,FR RoW_2
3 4 RoW,CA,US JP,BE,FR RoW_1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.