1

I have a large dataframe with ~450 columns. I need to export it to csv, and for that I use the next command:

df.to_csv('output.csv', sep='\t', header=False, index=False, na_rep='null')

The resulting file is missing several of the trailing columns. I tried to update pandas display options, but that didn't solve the problem:

pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', -1)
pd.set_option('display.width', None)

Any help is appreciated.


Background: I need to export data from one DB table to the other, adding one more column on the fly. I export data to csv, create a dataframe from it, add a column, and then need to export it to csv again to import into the resulting table.

3
  • Why can't you copy the dataframe and a column to the new df? Commented Dec 6, 2018 at 8:31
  • 2
    Have you tried setting <header=True> to check which columns are missing? alternatively, try using <sep=','> and see if issue persists. Commented Dec 6, 2018 at 8:32
  • Maybe it goes wrong when you're importing the file from DB to a df? Or do you have the right amount of columns when you do the import and it only goes wrong with the final export? Commented Dec 6, 2018 at 8:49

1 Answer 1

1

As it turned out, the original data had a quotation mark somewhere. This answer to another question helped me sort it out. I resolved the problem with:

pd.read_csv(file_obj, sep='\t', quoting=csv.QUOTE_NONE)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.