pandas - Python - take rows of data and place into single column -


i have following dataframe numerous rows. take multiple columns , condense 1 column.

player         |    0             |     1     |     2     | 3         |  4 edgerrin james | 1st tm all-conf. | ap 1st tm | fw 1st tm | sn 1st tm | pro bowl tony gonzalez  | 1st tm all-conf. | ap 1st tm | none      | none      | none ...            |  ...             | ...       | ...       | ...       | ... 

i'm trying figure out how restructure awards in 1 column. dataframe follows:

player         | awardid edgerrin james | 1st tm all-conf. edgerrin james | ap 1st tm edgerrin james | fw 1st tm  edgerrin james | sn 1st tm edgerrin james | pro bowl tony gonzalez  | 1st tm all-conf. tony gonzalez  | ap 1st tm 

if 'none' cells included, i'd fine because know how filter out after, can't figure out first part.

use set_index on player , stack

in [750]: df.set_index('player').stack().reset_index(name='awardid').drop('level_1', 1) out[750]:            player           awardid 0  edgerrin james  1st tm all-conf. 1  edgerrin james         ap 1st tm 2  edgerrin james         fw 1st tm 3  edgerrin james         sn 1st tm 4  edgerrin james          pro bowl 5   tony gonzalez  1st tm all-conf. 6   tony gonzalez         ap 1st tm 7   tony gonzalez              none 8   tony gonzalez              none 9   tony gonzalez              none 

optinally, remove none using query

in [757]: (df.set_index('player')              .stack()              .reset_index(name='awardid')              .drop('level_1', 1)              .query('awardid != "none"')) out[757]:            player           awardid 0  edgerrin james  1st tm all-conf. 1  edgerrin james         ap 1st tm 2  edgerrin james         fw 1st tm 3  edgerrin james         sn 1st tm 4  edgerrin james          pro bowl 5   tony gonzalez  1st tm all-conf. 6   tony gonzalez         ap 1st tm 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -