Ravi Krishna
Ravi Krishna

Reputation: 11

How to swap column values in pandas data frame

I have a csv file where I have my datas like [1]: https://i.sstatic.net/JMVBv.jpg Here in my case column vales were disordered. RollNo is exist under marks and vice versa. Name's were under Country column and vice versa. How can I swap the values in between the columns and fix it in proper order. Thanks in advance

Upvotes: 0

Views: 2230

Answers (3)

SeaBean
SeaBean

Reputation: 23227

If you just need an ad-hoc solution for one-time fix, you can do it quickly in one statement by:

df.columns = ['Total_marks', 'Country', 'Name', 'RollNo']

assuming df is the name of your dataframe.

Upvotes: 0

Joel Company
Joel Company

Reputation: 40

You can use the replace() function

df['column name'] = df['column name'].replace(['old value'],'new value')

Find the official docs here

Upvotes: 1

Oleksii
Oleksii

Reputation: 41

Maybe you just need to rename your columns?

import pandas as pd

df = pd.DataFrame({'a':[1,2,3,4], 'b': [3,4,5,6]})
df.rename(columns={'a': 'new_name'}, inplace=True)

Upvotes: 0

Related Questions