Using Pandas 0.25.3, trying to explode a couple of columns.
Data looks like:
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'}
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'
there are other columns as well with one value per, {'A':'B'}
type stuff, but I'm not worried about those.
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')
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
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