Home > OS >  Change a dataframe with one header to two headers
Change a dataframe with one header to two headers

Time:03-19

I have a dataframe and I want to change it to the others which I attached. The name of columns are not important, also the new one does not have index 0,1,2.... This seems rather obvious, but I can't seem to figure out it. Note: I don't want to change the values and columns. I only have the same structure. I've added a simple dataframe as an example:

import pandas as pd
df = pd.DataFrame()
df['timestamp'] =[ 1, 2]
df['cons_id'] = [2, 3]
df[ 'value'] = [ 4,5]

The dataframe which I have:

enter image description here

Here is the output that I want to change the dataframe to:

enter image description here

CodePudding user response:

Are you looking for set_index:

df = df.set_index('timestamp').rename_axis(columns='timestamp')
print(df)

# Output
timestamp  cons_id  value
timestamp                
1                2      4
2                3      5
  • Related