Home > other >  How to assign columns names in Pandas?
How to assign columns names in Pandas?

Time:06-04

I got the filtered Pandas table (dataframe). Is it possible to assign manually using UI interface the columns names and move fields across dataframe by mouse?

CodePudding user response:

Here's an example of how to change the order of columns in a table if that's your question

import pandas as pd

df = pd.DataFrame({
    'c':['c1','c2','c3'],
    'a':['a1','a2','a3'],
    'b':['b1','b2','b3'],
})

print('Original column order')
print(df)
#Original column order
#    c   a   b
#0  c1  a1  b1
#1  c2  a2  b2
#2  c3  a3  b3


reordered_df = df[['a','b','c']]
print('Reorderd columns')
print(reordered_df)
#Reorderd columns
#    a   b   c
#0  a1  b1  c1
#1  a2  b2  c2
#2  a3  b3  c3

CodePudding user response:

If you need to rename columns , you can do the following :

df.rename(columns={'col_a':'new_col_a'}, inplace=True)

For alignment you can simply follow @mitoRibo suggestion.

  • Related