### I create `swapColumns` function
It swaps the position of the two columns in the DataFrame and then renames the columns to reflect the swap.
xxxxxxxxxx
def swapColumns(df, col1, col2):
# Get the list of column names in the DataFrame
cols = df.columns
# Get the index of the two columns to swap
col1_idx = cols.get_loc(col1)
col2_idx = cols.get_loc(col2)
# Swap the two columns using the index
df[[cols[col1_idx], cols[col2_idx]]] = df[[cols[col2_idx], cols[col1_idx]]]
# Rename the columns to reflect the swap
df.rename(columns = {col1:col2, col2:col1}, inplace = True)
# To use the function, you can pass in the DataFrame df and the two column names col1 and col2 like this :
swapColumns(df, 'longitude', 'median_house_value')
This will swap the position of the longitude and median_house_value columns in the DataFrame df.
xxxxxxxxxx
# 2d array a[row][col] , swap the columns h and k
for i in range(row):
a[i][k] , a[i][h] = a[i][h], a[i][k]
xxxxxxxxxx
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