switch columns python pd dataframe code example
Example 1: print columns pandas
df = pd.DataFrame(exam_data , index=labels)
# print the columns labeled "name" and "score"
print(df[['name', 'score']])
Example 2: rearange DataFrame swap columsn and rows
df_t = df.T
print(df_t)
# 0 1 2
# name Alice Bob Charlie
# age 24 42 18
# state NY CA CA
# point 64 92 70
Example 3: dataframe index column invert
In [11]: df
Out[11]:
10 20 30 70
data1: 2.3 5 6 7
In [12]: df.T
Out[12]:
data1:
10 2.3
20 5.0
30 6.0
70 7.0