CSV

Comma Separated Variables (CSV)

CSV (comma separated values) is a text-only spreadsheet format. Most database and spreadsheet programs can use them or create them.

  • The first row contains column headings.
  • Other rows contain values.
  • Each column heading and each variable is separated by a comma:
column1,column2,column3
value1,value2,value3

Load CSV

We can load CSV into a DataFrame in Pandas using pd.read_csv()

pd.read_csv('csv-file.csv')

We can display a DataFrame with a print() function.

For example: To read the CSV ‘sample.csv’ into a variable called df, we use the following code:

import pandas as pd
df = pd.read_csv('sample.csv')
print(df)

When we print DataFrame, we can see all rows of DataFrame.

Save CSV

We can also save data to a CSV, using .to_csv()

df.to_csv('new-csv-file.csv')