Load and Save a CSV File in Python

python

This is a very short post related to loading and saving csv files in python. I am always confused with the syntax and mix up python’s with R syntax. So here is to not be confused anymore!

Let’s import some packages

# Import
import pandas as pd

Let’s load a csv file

# Let's load the Iris dataset saved locally on my computer
df = pd.read_csv("iris.csv")
df.head()
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa

Saving a DataFrame into a csv file

df.to_csv("dataframe.csv")

Et voila!

Share this post: