从两列中创建一个数据帧,每个列都包含列表

2024-04-19 16:02:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个这样的文件:

Location Code   Trait ID    Effective Date
WAU1    23984,24896,27576   06/05/2014 ,06/05/2014 ,06/12/2014 
WAU2    126973,219332   06/05/2014 ,06/05/2014 
WAU3    24375   06/05/2014 
WAU4    23984   06/05/2014 
WAU5    5199,23984  NULL
WAU6    12342,224123    06/05/2014 

请注意,第2列和第3列是一个值的“列表”。有些行包含每个列表中元素数量的精确匹配,有些行丢失或根本不存在(null)。我需要创建一个类似于下面的数据帧

   Location Code Trait ID  Effective Date
       0    WAU1    23984   06/05/2014
       1    WAU1    24896   06/05/2014
       2    WAU1    27576   06/12/2014
       3    WAU2    126973  06/05/2014
       4    WAU2    219332  06/05/2014
       5    WAU3    24375   06/05/2014
       6    WAU4    23984   06/05/2014
       7    WAU5    5199    NaN
       8    WAU5    23984   NaN
       9    WAU6    12342   06/05/2014
       10   WAU6    224123  NaN

我已经能够使用以下方法将每个“列表”列分解为单独的数据帧:

df1 = df1['Trait ID'].str.split(',').apply(pd.Series,1).stack()
df1.index = df1.index.droplevel(-1)
df1.name = 'Trait ID'
del df1['Trait ID']
df1 = df1.join(trait_id)

给我的感觉是:

  Location Code Trait ID
0          WAU1    23984
0          WAU1    24896
0          WAU1    27576
1          WAU2   126973
1          WAU2   219332
2          WAU3    24375
3          WAU4    23984
4          WAU5     5199
4          WAU5    23984
5          WAU6    12342
5          WAU6   224123

我可以使用上面相同的逻辑创建另一个具有“生效日期”列表的数据帧,以生成以下内容:

  Location Code Effective Date
0          WAU1    06/05/2014 
0          WAU1    06/05/2014 
0          WAU1    06/12/2014 
1          WAU2    06/05/2014 
1          WAU2    06/05/2014 
2          WAU3    06/05/2014 
3          WAU4    06/05/2014 
4          WAU5            NaN
5          WAU6    06/05/2014 

我正在努力在pandas中找到合适的“函数”(例如join、merge、concat)来将两个数据帧合并到我想要的输出中。虽然我觉得这是它们的结合,在某个地方有一个reset_index()。你知道吗


Tags: 数据id列表codelocationnandf1effective
1条回答
网友
1楼 · 发布于 2024-04-19 16:02:16

开始于:

  Location Code             Trait ID                    Effective Date
0          WAU1  23984, 24896, 27576  06/05/2014,06/05/2014,06/12/2014
1          WAU2       126973, 219332             06/05/2014,06/05/2014
2          WAU3                24375               2014-06-05 00:00:00
3          WAU4                23984               2014-06-05 00:00:00
4          WAU5          5199, 23984                               NaN
5          WAU6        12342, 224123               2014-06-05 00:00:00

您可以groupby('Location Code'),对每个组使用str.split(',') withexpand=True , pivot the result usingstack()andconcat`

df1.groupby('Location Code').apply(lambda x: pd.concat([x['Trait ID'].str.split(',', expand=True).stack(), x['Effective Date'].str.split(',', expand=True).stack()], axis=1)).reset_index([1, 2], drop=True)

要获得:

                     0                    1
Location Code                              
WAU1             23984           06/05/2014
WAU1             24896           06/05/2014
WAU1             27576           06/12/2014
WAU2            126973           06/05/2014
WAU2            219332           06/05/2014
WAU3             24375  2014-06-05 00:00:00
WAU4             23984  2014-06-05 00:00:00
WAU5              5199                  nan
WAU5             23984                  NaN
WAU6             12342  2014-06-05 00:00:00
WAU6            224123                  NaN

相关问题 更多 >