When transposing a pandas Dataframe which has a named index, the previous index name ( for the index in the first column ) appears as the first entry for the column names.
Example:
original DataFrame df_1
| 1800 | 1801 | 1802 | |
|---|---|---|---|
| country | |||
| Germany | 38.4 | 38.4 | 38.4 |
df_2 = df_1.T
df_2 is
| country | Germany |
|---|---|
| 1800 | 38.4 |
| 1801 | 38.4 |
| 1802 | 38.4 |
First question. Why is country now the index for 1800, 1801 etc. and is there a better tranpose option to avoid this?
When trying to rename the index with
df_2.index.set_names(["year"],inplace=True)
the following result is shown
| country | Germany |
|---|---|
| year | |
| 1800 | 38.4 |
| 1801 | 38.4 |
| 1802 | 38.4 |
Question two. Why is country still there and how to remove it?