Python Forum
renaming a column without a name in a dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
renaming a column without a name in a dataframe
#1
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.

Attached Files

Thumbnail(s)
   

.csv   Inputfile.csv (Size: 236 bytes / Downloads: 52)
Reply
#2
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)
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information automatic document renaming lisa_d 2 1,458 Mar-20-2024, 06:34 PM
Last Post: Pedroski55
  Adding PD DataFrame column bsben 2 1,333 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 1,384 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Difference one column in a dataframe Scott 0 1,174 Feb-10-2023, 08:41 AM
Last Post: Scott
  splitting a Dataframe Column in two parts nafshar 2 1,815 Jan-30-2023, 01:19 PM
Last Post: nafshar
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 1,905 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  renaming the 0 column in a dataframe Led_Zeppelin 5 6,969 Aug-16-2022, 04:07 PM
Last Post: deanhystad
  Copy a column from one dataframe to another dataframe Led_Zeppelin 17 20,168 Jul-08-2022, 08:40 PM
Last Post: deanhystad
  Cannot convert the series to <class 'int'> when trying to create new dataframe column Mark17 3 10,384 Jan-20-2022, 05:15 PM
Last Post: deanhystad
  Functions to consider for file renaming and moving around directories cubangt 2 2,674 Jan-07-2022, 02:16 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020