Import CSV data in Python (import pandas)

CSV means “comma separated values.”

Example of what CSV data looks like:

name, age, points
Joy, 15, 1025
Alfred, 14, 900
Jane, 16, 800

You can view a .csv file in Excel to see it in a grid. You can also see it in any text editor such as Notepad++ or Sublime Text.

You can create a .csv file from Excel or Google Spreadsheets if you save as .csv instead of using the regular save command.

Loading .csv data in Python using “pandas”

import pandas as pd
data = pd.read_csv('my_file.csv')
names = data.name
ages = data.age
points = data.points

In this example, the column names from the data file (name, age, points) are used in the commands.

It won’t work if you say “data.things” since there is not a column with the heading “things” in your CSV file.

Now you can index the names by using references like “names[3]”

If you need a plain Python list, you can use the list() function to convert it to a list:

import pandas as pd
data = pd.read_csv('my_file.csv')
names = list(data.name)