-3

I want to change the shape of the following table

input table

to

expected result

using pandas dataframe. How can I do it?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • 1
    Does this answer your question? [Transpose a data frame](https://stackoverflow.com/questions/6778908/transpose-a-data-frame) – Dhana D. Aug 14 '21 at 04:08

2 Answers2

-1

You can use this transpose function:

numpy.transpose(df)
-1

You can use the following for your case supposing that the data frame that you are using is called df and you still want to preserve the column labels (titles):

import pandas as pd
data = {'Country/Region': [1, 2], 'Afghanistan': [3, 4]} # This is mock data
df = pd.DataFrame(data)
headers = list(df.iloc[:, 0])
indicies = list(range(len(headers)))
renamed = dict(zip(indicies, headers))
sub_df = df[['Afghanistan']]
sub_df.rename(columns=renamed, inplace=True)
result = sub_df.transpose()
print(result)
Bilal Qandeel
  • 727
  • 3
  • 6