Pandas DataFrames can be easily read from and written to CSV files using the read_csv() and to_csv() methods.
Reading a CSV file into a Pandas DataFrame
To read a CSV file into a Pandas DataFrame, you can use the read_csv() method. The read_csv() method takes the path to the CSV file as input and returns a Pandas DataFrame.
Here is an example of how to read a CSV file into a Pandas DataFrame:
import pandas as pd df = pd.read_csv('data.csv') print(df)
This will read the CSV file data.csv into a Pandas DataFrame and print the DataFrame to the console.
Writing a Pandas DataFrame to a CSV file
To write a Pandas DataFrame to a CSV file, you can use the to_csv() method. The to_csv() method takes the path to the CSV file as input and writes the Pandas DataFrame to the file.
Here is an example of how to write a Pandas DataFrame to a CSV file:
import pandas as pd df = pd.DataFrame({'name': ['Alice', 'Bob', 'Carol'], 'age': [25, 30, 35]}) df.to_csv('data.csv', index=False)
This will write the Pandas DataFrame df to the CSV file data.csv without the index.