I am trying to write a pandas df as csv file using to_csv function. After writing, I observed that pandas automatically creates default column names like below.
_c0 _c1 _c2 _c3 _c4 _c5
0 A B C D E F
1 a b c d e f
2 g h i j k l
My actual column names are [A,B,C,D,E,F]. The following is my write function
df.to_csv(file_name,encoding='utf-8',header=True,index=False,columns=[A,B,C,D,E,F])
My expected format should be as follows
A B C D E F
0 a b c d e f
1 g h i j k l
I can get the expected format by executing the following lines after reading the created csv.
df.columns = df.iloc[0]
df = df.iloc[1:]
But, is there any way I can avoid the creation of these default column names before creating the csv ?
Thanks in advance
I think the "problem" is related to when you have imported the dataframe. You did not set up the header properly. I think you cannot find the desired columns names in your df, don't you? How did you import this?
If you are reading it from a csv and use pd.read_csv make sure header=0