Python Forum

Full Version: renaming a column without a name in a dataframe
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've got the following data (see attachement) from a testing rig and can't change the output I get.
I want to name the first column, but can't figure out how.
I'm not talking about the index, but about date & time.

I've tried using the "name" given ('Unnamed: 0'), but df.rename does not work for me.
I've tried pulling the column names with df.columns, but can't change the 'Unnamed: 0' string without destroying my whole index.
I've tried accessing it with [i]iloc[i], but can't figure out how to access the header before row 0.

Also attached is a random .csv as example with the read_csv I use:
df_Data = pd.read_csv(path_Data, sep=';', encoding = "ISO-8859-1", parse_dates=[0], date_format="%d.%m.%Y %H:%M")

Any help is appreciated. Thanks in advance.
Why don't you just edit the csv?

import pandas as pd

path2csv = '/home/pedro/temp/Inputfile.csv'
df1 = pd.read_csv(path2csv, sep=';', encoding = "ISO-8859-1", parse_dates=[0], date_format="%d.%m.%Y %H:%M")
# show the column names
df1.columns # first column is 'Unnamed: 0'
# change the name of the column in the display
df1.rename(columns={"Unnamed: 0": "Date"})
df1.columns # still shows first column as 'Unnamed: 0'
df2 = pd.read_csv(path2csv, sep=';', encoding = "ISO-8859-1", parse_dates=[0], date_format="%d.%m.%Y %H:%M")
# use inplace='True'
df2.rename(columns={"Unnamed: 0": "Date"}, inplace='True')
df2.columns # the first column is now Date
# check the output file you will see Date as the first column
df2.to_csv('/home/pedro/temp/output_file.csv', index=False)
I have no idea why this did not work before as I tried this in several ways, but now it does work, so thank you.