Skip to content
Advertisement

Dropping the index column from DataFrame in a .csv file in Pandas

I have a python script here:

and when I run it, it creates separate csv files that are formatted in sql. The output looks like this in generatedfile2:

The rest of the files have this same format. Is there any way I can change my code to get rid of the “2” at the beginning of the code? It won’t run properly in SQL because of it. For some reason index_label=False won’t get rid of it. Thanks a bunch!

Advertisement

Answer

When running DataFrame.iterrows, row renders as a Pandas Series and not a Data Frame. So actually, you are running Series.to_csv.

However, this method is not useful to iterate off a single column. Instead, consider simply looping directly on the values of the column (assumed to be entirely your SQL statement). With each iteration, create the corresponding csv file with typical open + write methods. Use enumerate for sequential numbering for file suffixes.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement