Python pandas – CSV to DataFrame

In this post we will be creating a Python script that will read CSV data to a pandas DataFrame. To do this we will be using the pandas ‘read_csv()’ function which reads a comma-separated values file in to a DataFrame.

See the snippet of code below which reads a CSV file named ‘Sample.csv’ to a DataFrame.

import pandas as pd

df = pd.read_csv('Sample.csv', sep=',')
print(df)

The above will treat the top row as column headers, if your CSV does not contain any column headings use the snippet of code below instead.

import pandas as pd

df = pd.read_csv('Sample.csv', sep=',', header=None )
print(df)

The above will add numbered column headings to your DataFrame with a starting point of ‘0’.

Take a look at some of our other content around the Python programming language here. Or check out our Pandas crash course by clicking here instead.

Leave a Reply