'Change a dataframe with one header to two headers
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:
Here is the output that I want to change the dataframe to:
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


