Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas won't rename the columns

I am trying to change the name of the columns from "0" to "A" and "1" to "B".

import pandas as pd
import numpy

data = pd.DataFrame()

data = pd.read_csv('rakett.txt', sep="  ", header=None)

data = data.rename(columns={'0': 'newName1', '1': 'newName2'})

print(data)

However when i open the dataframe nothing is changed…
Iamge of dataframe when opened in the variabel explorer

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The code you’ve provided should work for renaming the columns, but it seems there might be an issue with the way the data is loaded from your CSV file. If the column names are not changing as expected, it could be because the column names in the DataFrame are still the default integer-based names.

You can rename the columns after reading the data from the CSV file by specifying the new column names as a list. Here’s how you can do it:

import pandas as pd

data = pd.read_csv('rakett.txt', sep="  ", header=None)

# Rename the columns
data.columns = ['A', 'B']

print(data)

This code reads the data from ‘rakett.txt’ and then directly assigns the new column names ‘A’ and ‘B’ to the DataFrame.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading