Home > Software engineering >  how to avoid row number in read_sql output
how to avoid row number in read_sql output

Time:06-15

When I use pandas read_sql to read from mysql, it returns rows with row number as first column as given below. Is this possible to avoid row numbers?

enter image description here

CodePudding user response:

You can use False as the second parameter to exclude indexing.

Example

df.to_csv('filename.csv', index = False, encoding='utf-8')
print(df)

or

df.toPandas().to_csv('filename.csv', index=False, encoding='utf-8')
print(df)

Use this function to guide you
DataFrame.to_csv(self, path_or_buf=None, sep=', ', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, mode='w', encoding=None, compression='infer', quoting=None, quotechar='"', line_terminator=None, chunksize=None, date_format=None, doublequote=True, escapechar=None, decimal='.')

You can read more about this here -> Pandas DataFrame: to_csv() function

CodePudding user response:

Thanks All. I need to export the rows into CSV to visualizer. So row numbers are not required. in the csv function of pandas, I have set index=False to avoid index being shown. and that worked. Thank you all for your help. Cheers

df.toPandas().to_csv('output.csv', index=False)
  • Related