mercredi 29 juin 2016

convert to multiindex dataframe w/horizontal display and rename columns


let's say i have the following code: df1 = pd.DataFrame(np.random.randn(10,4), columns=['A','B','C','D']) df1['dataframe'] = 'df1' df2 = pd.DataFrame(np.random.randn(10,4), columns=['A','B','C','D']) df2['dataframe'] = 'df2' df = pd.concat([df1, df2]) df.reset_index().set_index(['dataframe','index']) this will return me a dataframe with 2 levels of indices, 'dataframe' and 'index'. i'm not sure what the correct term is but visually, the first index spans across rows as opposed to columns. there are 2 operations i would like to perform on this dataframe that i am struggling with. 1) i would like to rename the columns in each "sub-dataframe" to something different, taken from a different list and apply them accordingly based on the first index previously assigned. i have tried the following but it does not work if i display "df" again: new_cols = ['df1', 'df2'] for i,x in enumerate(new_cols): old_cols = df.loc[x].columns.tolist() df.loc[x].rename(columns={col_label: '{}_{}'.format(x,col_label) for col_label in old_cols}, inplace=True) so, to be clear, instead of A,B,C,D i'd like df1_A...df1_D and df2_A...df2_D 2) i would like to re-orient this dataframe such that they span across the columns and so i would be scrolling across in order to view each "sub-dataframe" rather than up and down. i've consulted the pandas API but still not able to get this right.

Aucun commentaire:

Enregistrer un commentaire