Home > front end >  How to align the CSV column values to left side using python pandas
How to align the CSV column values to left side using python pandas

Time:04-21

[enter image description here]

As shown in the screen shot, I want to align the B-column values to the left side & python script should save to the same csv file.

CodePudding user response:

.csv-files have no formating information. If you want to write the dataframe in an excel with an alignment, please try this:

pandas-dataframe-to-excel-vertical-alignment-of-index

Maybe is that's a possible solution for you. You have to edit format.set_align('vcenter') to format.set_align('left')

You get additional information here => set_align()

Try it and give me feed, if it works :)

CodePudding user response:

CSVs have no formatting, so that is not possible. However, if you are wanting an excel file, this will use xlsx writer's formats, the documentation for this can be found here.

You need to use pd.ExcelWriter, write your df and create a format to left align (you can add more formatting if required) and set the columns A to C to be left aligned as below:

writer = pd.ExcelWriter('filename.xlsx')
df.to_excel(writer)
left = workbook.add_format({'align' : 'left'})
worksheet.set_column('A:C', 8, center)
writer.save()
  • Related