I want to change the shape of the following table
to
using pandas dataframe. How can I do it?
You can use this transpose
function:
numpy.transpose(df)
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)