Home > OS >  DataFrame and Python don't find the columns' names of a CSV file
DataFrame and Python don't find the columns' names of a CSV file

Time:09-26

I use DataFrame in Python in order to manipulate CSV files. So I use things like df['column_name'].

but df doesn't seem to find the column in the file (writes a "KeyError"), even if there really is that column, and even if I checked it back to see if there was a mistake in the letters. So if I want my program to work, and my CSV file to be readen by df and python, I need to manually change the name of the column that I want to manipulate before everything. To explain the situation, the files I manipulate are not from me, they're pregenerated, and so it looks like python doesn't want to read it if I don't change the name of the column, because everything works after changing it. I hope you have understood and you'll be able to help me !

CodePudding user response:

Have you checked if the 'column_name' in file and code, both are in capital or both are in small letters? sometimes it gives error when one is in capital but another one is in small letters.

CodePudding user response:

have you checked for white spaces? like tabs or line breaks.

CodePudding user response:

ok, I figured the thing out but I don't know how to deal with it: I get that, from the file I want to manipulate, if I copy the column named "Time":

(Time
)

I added brackets just to show that it goes to the line right after, so it seems to be a problem from the original file, with the name of the column having a "enter" literaly in itself. So it makes that for example in a code:

time = df['Time
']

It prevents the code from working. I don't have any idea about how to deal with it, and I don't think I can fix it by fixing the column's name in the file because it is pregenerated.

  • Related