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
>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.