python - Splice and combine two columns to form a new data frame (Pandas) -


i need convert pandas dataframe little odd list. have following example pandas dataframe:

input dataframe:

mydf= pd.dataframe.from_dict({'ars':['xx2','xx3','xx1'], 'xyz':['yy1','xx2','xx3'], 'ppp':['xx3','yy2','xx2']}, orient='columns') mydf= mydf.stack().reset_index() mydf.columns= ['list1','list2','list3'] newdf= mydf[['list2','list3']] newdf    list2 list3 0   ars   xx2 1   ppp   xx3 2   xyz   yy1 3   ars   xx3 4   ppp   yy2 5   xyz   xx2 6   ars   xx1 7   ppp   xx2 8   xyz   xx3 

desired dataframe:

>ars xx2 xx3 xx1 >ppp xx3 yy2 xx2 >xyz yy1 xx2 xx3 

does have simple pandas way convert this?

here's pandas way using groupby, pd.concat indexing:

(newdf.groupby('list2',as_index=false)      .apply(lambda x: pd.concat([pd.series(x.iloc[0]['list2']),                                  pd.series(x.loc[:,'list3'])]))     .reset_index(drop=true)) 

output:

0     ars 1     xx2 2     xx3 3     xx1 4     ppp 5     xx3 6     yy2 7     xx2 8     xyz 9     yy1 10    xx2 11    xx3 dtype: object 

if wanted '>' sign use follow:

(newdf.groupby('list2',as_index=false)      .apply(lambda x: pd.concat([pd.series('>'+x.iloc[0]['list2']),                                  pd.series(x.loc[:,'list3'])]))     .reset_index(drop=true)) 

output:

0     >ars 1      xx2 2      xx3 3      xx1 4     >ppp 5      xx3 6      yy2 7      xx2 8     >xyz 9      yy1 10     xx2 11     xx3 dtype: object 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -