0

[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.

Mohan Raj
  • 1
  • 1

2 Answers2

0

.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 :)

Christian01
  • 307
  • 1
  • 5
  • 19
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 21 '22 at 08:12
0

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()
Emi OB
  • 2,814
  • 3
  • 13
  • 29