0

When I export a pandas dataframe to .csv files, some of the column titles are not fully displayed e.g., "distance from" instead of "distance from portal". Also these columns are not fully displyed in the dataframe.

Note: these columns are imported(red) as dataframe and merged into other dataframe. The original dataframe columns' titles are fully displayed but those from the newly annexed dataframe are not.

I was trying for exporting the dataframe:
df2.to_csv(rf"D:\New_folder\New_folder\MWD_AND_EXP.csv")

I was also trying for expanding the column head cells in the dataframe:
pd.set_option('display.max_colwidth', None)

however, I have also a problem with the column head height not just the width

Adrian Ang
  • 520
  • 5
  • 12
Abboud
  • 9
  • 4
  • Please explain if the problem occurs in the dataframe or that in the dataframe the names are ok but wrong after saving to CSV – gtomer Jul 23 '23 at 13:04
  • the problem occurs in both places such that; in the dataframe the columns names of the annexed dataframe are not fully displayed , and when the whole dataframe is exported to .csv file, the columns names of the annexed dataframe are not fully displayed as well. – Abboud Jul 23 '23 at 13:10
  • 2
    If the error is occurring during import - then of course it will remain during export. Check the CSV if the column names are not divided into two cells. Otherwise it should work ok – gtomer Jul 23 '23 at 13:13
  • 1
    What do you mean displayed? CSV is a text file without any display property. By *display*, you may be referring to software like an IDE (VisualStudio, Spyder), or Excel or web interface like Jupyter. To be clear, `to_csv` should not cut off names but may *display* a truncated version per settings in desktop or web app. Please post the columns of DataFrame (`df.columns.tolist()`) and headers of CSV text (copy first row from text editor). – Parfait Jul 23 '23 at 17:57
  • It sounds like the problem is not where you think it is, so please make a [mre]. For specifics, see [How to make good reproducible pandas examples](/q/20109391/4518341). – wjandrea Jul 23 '23 at 20:20

1 Answers1

0

Try something like -

df2.to_csv(rf"D:\New_folder\New_folder\MWD_AND_EXP.csv", index_label="full column names")

Do extract first row, then give that as column names as second argument index_label value.