Saving processed Dataset to a CSV File using Pandas
Pandas: Machine Learning Integration Exercise-16 with Solution
Write a Pandas program to save the processed Dataset to a CSV file.
This exercise shows how to save the processed dataset to a CSV file using to_csv().
Sample Solution :
Code :
import pandas as pd
# Load the dataset
df = pd.read_csv('data.csv')
# Perform some data preprocessing (e.g., fill missing values)
df['Age'].fillna(df['Age'].mean(), inplace=True)
# Save the processed dataset to a CSV file
df.to_csv('processed_data.csv', index=False)
# Confirm the dataset is saved
print("Processed dataset saved to 'processed_data.csv'.")
Output:
Processed dataset saved to 'processed_data.csv'.
Explanation:
- Loaded the dataset using Pandas.
- Performed basic data preprocessing (filled missing values).
- Used to_csv() to save the processed dataset to a CSV file.
- Confirmed that the file was saved successfully.
Python-Pandas Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/pandas/pandas-save-processed-dataset-to-csv-file.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics