Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

This is my data frame 

import pandas as pd

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

I wanted to convert the data frame to CSV and also want to change names of columns for example:

numrows numcols note

1 2

3 4

Should I manually do it? or will pandas help me solve this? 

1 Answer

0 votes
by (36.8k points)

You need to first create a CSV file then write into the file and then append it to the data frame.

with open('file.csv', 'a') as file:

    file.write('Custom String\n')

    df.to_csv(file, header=False, index=False)

Click on this to see a similar solution.

You can use the below code in your case:

with open('file.csv', 'a') as file:

    file.write('numrows numcols note\n')

    df.to_csv(file, header=False, index=False)

Learn Python for Data Science Course to improve your technical knowledge. 

Related questions

Browse Categories

...