我有两个数据框,其中包含流行商店及其所在地区的数据。每个商店都是连锁店,可能有多个地区位置id(例如,“Store1”在不同的地方有多个商店)
First df有关于前5名最受欢迎的商店和地区ID的信息,这些信息用分号分隔,例如:
store_name district_id
Store1 | 1;2;3;4;5
Store2 | 1;2
Store3 | 3
Store4 | 4;7;10;15
Store5 | 12;15;
第二个df只有两列,包含城市中的所有地区,每行都是唯一的地区id和名称
district_id district_name
1 | District1
2 | District2
3 | District3
4 | District4
5 | District5
6 | District6
7 | District7
8 | District8
9 | District9
10 | District10
etc.
目标是在df1中为top-5中的每个商店创建列,并将每个地区id号与地区名称匹配
首先,我将df1拆分为如下形式:
store_name district_id 0 1 2 3 4 5
Store1 | 1 | 2 | 3 | 4 | 5
Store2 | 1 | 2 | | |
Store3 | 3 | | | |
Store4 | 4 | 7 | 10| 15|
Store5 | 12 | 15|
但现在我被绊住了,不知道如何匹配从df1到df2的每个值,并获取每个id的地区名称。空单元格是无的,因为列是由每个商店的最大值创建的
我想得到这样的df:
store_name district_name district_name2 district_name3 district_name4 district_name5
Store1 | District1 | District2 | District3 | District4 | District5
Store2 | District1 | District2 | | |
Store3 | District3 | | | |
Store4 | District4 | District7 | District10 | District15 |
Store5 | District12 | District15 | | |
提前谢谢
所以有很多方法可以做到这一点,这只是其中之一。假设您将两个数据帧存储为df1和df2:
首先,规范化df1中的district_id列,使其长度相同:
然后将文本列拆分为单独的列并删除原始列:
然后获取df2中ID到其各自名称的映射,并使用该映射替换新列中的值:
就像我说的,我相信还有其他方法可以做到这一点,但这应该是可行的
您可以
stack
第一个数据帧,然后将其转换为浮点类型,map
第二个数据帧中的列,然后unstack
最后add_prefix
:输出:
用于上述代码的数据帧:
步骤1:使用
explode()
将值拆分为行步骤2:将
merge()
与on='district_id'
一起使用步骤3:使用
groupby()
&agg()
以获取包含列表的列然后,它可以根据需要进行拆分
相关问题 更多 >
编程相关推荐