问题描述
使用Pandas 0.25.3,尝试爆炸几列.
Using Pandas 0.25.3, trying to explode a couple of columns.
数据如下:
d1 = {'user':['user1','user2','user3','user4'],
'paid':['Y','Y','N','N']
'last_active':['11 Jul 2019','23 Sep 2018','08 Dec 2019','03 Mar 2018'],
'col4':'data'}
我将其发送到如下所示的数据帧df=pd.DataFrame([d1],columns=d1.keys())
:
I sent this to a dataframe df=pd.DataFrame([d1],columns=d1.keys())
that looks like this:
user paid last_active col4
['user1','user2','user3','user4'] ['Y','Y','N','N'] ['11 Jul 2019','23 Sep 2018','08 Dec 2019','03 Mar 2018'] 'data'
还有其他列,每个{'A':'B'}
类型的东西都有一个值,但是我并不担心这些.
there are other columns as well with one value per, {'A':'B'}
type stuff, but I'm not worried about those.
当我执行df.explode('user')
时,它适用于该列,而其他列则相同,但是当我尝试执行df.explode(column=('user','paid','last_active')
时,它会给我以下错误:
when I do df.explode('user')
it works for that one, and same for the other columns, but when I try to do df.explode(column=('user','paid','last_active')
it gives me the following error:
KeyError: ('user','paid','last_active')
所以我想知道的是如何使用多列上的explode
函数将其爆炸以获取以下df:
So what I want to know, is how can I explode it with the explode
function on multiple columns to get the following df:
user paid last_active col4
'user1' 'Y' '11 Jul 2019' 'data'
'user2' 'Y' '23 Sep 2018' NaN
'user3' 'N' '08 Dec 2019' NaN
'user4' 'N' '03 Mar 2018' NaN
推荐答案
我想您需要注意(请注意col4
的数据差异,其中None
如OP所述):
I guess you need (note the difference in data for col4
which has None
as OP mentioned):
pd.DataFrame([[i] if not isinstance(i,list) else i
for i in d1.values()],index=d1.keys()).T
user paid last_active col4
0 user1 Y 11 Jul 2019 data
1 user2 Y 23 Sep 2018 None
2 user3 N 08 Dec 2019 None
3 user4 N 03 Mar 2018 None
这篇关于 pandas 在多列爆炸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!