8

I have a dataframe

import pandas as pd
df = pd.DataFrame([[1, 2], [3, 4]], columns=['a', 'b'])

I want to write df to a csv file but not using the columns ['a', 'b']. The first line is my custom string and the rest are the content of df.values. For example:

numrows numcols note
1 2
3 4

Can I do this with pandas or I have to manually loop through the content and write to file?

THN
  • 3,011
  • 3
  • 23
  • 37
  • 2
    you can rename columns in dataframe before you save it. – furas May 16 '19 at 10:44
  • why don't check documentation for [dataframe.to_csv()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html). There is option `header` - maybe it renames headers in file. – furas May 16 '19 at 10:46
  • 1
    @furas I want to write a custom string, it's not the same number of columns with the data content. See my example above. – THN May 16 '19 at 10:49
  • check `header` again - you can use different names than in columns. – furas May 16 '19 at 10:52
  • @furas header can change value, but the number of columns must not change. – THN May 16 '19 at 11:01
  • if you add empty column then you will have three columns with three headers - and you can normally save it . But in file you will see three headers with two columns - last empty column will be invisible for you. – furas May 16 '19 at 11:05

3 Answers3

8

You can first create a csv file with the custom text in the first line, and then append the dataframe to it.

with open('file.csv', 'a') as file:
    file.write('Custom String\n')
    df.to_csv(file, header=False, index=False)

Also, see this post.

So, in your case, just use this

with open('file.csv', 'a') as file:
    file.write('numrows numcols note\n')
    df.to_csv(file, header=False, index=False)
  • This is a principled solution, and works well when setting the write-mode correctly. I'm not sure which answer to accept. Will accept by the vote count later. – THN May 16 '19 at 11:07
7

Improving over @Divyanshu Srivastava answer:

Not that it matters a lot, but no need for keeping open files:

with open(file_path, 'w') as f:
     f.write('Custom String\n')

df.to_csv(file_path, header=False, mode="a")
Zionsof
  • 1,066
  • 8
  • 19
1

First write custom string and then all data without columns in append mode:

file = 'file.csv'
pd.DataFrame(columns=['numrows numcols note']).to_csv(file, index=False)
df.to_csv(file, header=None, index=False, mode='a')
jezrael
  • 729,927
  • 78
  • 1,141
  • 1,090