Home > Net >  how do you transform dataframes in python pandas
how do you transform dataframes in python pandas

Time:04-19

I have this data frame:

key         value
EXE_PATH     /opt/IBM/ITM/aix526/ux/bin/kuxagent 
EXE_NAME      kuxagent 

I need to transform this data frame to be like this. Key values need to be column names and values need to be row entries

EXE_PATH                             EXE_NAME
/opt/IBM/ITM/aix526/ux/bin/kuxagent   kuxagent 

CodePudding user response:

You can do a transpose, with some additional steps to clean up the key and value:

df.set_index('key').T.reset_index(drop=True)

But perhaps a better question to ask is how did you end up with the first dataframe and if it is possible to work on the input before trying to fight pandas in how it represents your data as a dataframe.

CodePudding user response:

If df looks like this.

key value
0 EXE_PATH /opt/IBM/ITM/aix526/ux/bin/kuxagent
1 EXE_NAME kuxagent

You can make the key's the columns and the values the rows using the code below.

new_df = df.T.iloc[1:, :]
new_df.columns = df["key"]
new_df 
  • Related