W3docs

Writing a pandas DataFrame to CSV file

In the above code snippet, the to_csv method is used to write a DataFrame to a CSV file.

Writing a pandas DataFrame to CSV file

import pandas as pd

# create a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

# write the dataframe to a CSV file
df.to_csv('sample.csv', index=False)

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

In the above code snippet, the to_csv method is used to write a DataFrame to a CSV file. The first argument to the method is the file path where the CSV file will be saved. The index=False argument is used to prevent the DataFrame's index from being written to the file.