Пример :
df = pd.DataFrame(np.random.rand(4,3), columns=list('abc'))
Здесь проблема District
не столбец, по имени оси, поэтому вы можете выбрать его по df.index
:
finaldf1=df.T
print (finaldf1)
District Active Confirmed Deceased Recovered
a 0.716292 0.672918 0.559143 0.206168
b 0.448028 0.196304 0.831332 0.511764
c 0.048026 0.943950 0.339930 0.990788
print (finaldf1.columns)
Index(['Active', 'Confirmed', 'Deceased', 'Recovered'], dtype='object', name='District')
print (finaldf1.index)
Index(['a', 'b', 'c'], dtype='object')
print (finaldf1.index.name)
None
print (finaldf1.columns.name)
District
Для столбца необходимо DataFrame.rename_axis
с DataFrame.reset_index
:
df.index = pd.Index(['Active','Confirmed','Deceased','Recovered'],name='District')
finaldf=df.T.rename_axis(index='District', columns=None).reset_index()
print (finaldf)
District Active Confirmed Deceased Recovered
0 a 0.700793 0.722742 0.452414 0.507035
1 b 0.669347 0.853931 0.296660 0.370256
2 c 0.552846 0.609019 0.630620 0.138213