How to open a CSV file using pandas
For all of these examples, before we do anything we run import pandas as pd
. This allows us to use pandas! We use the as pd
part because everyone is too lazy to type out p-a-n-d-a-s so this renames it to pd
instead.
Opening a basic CSV file
To open a basic CSV file, you can use .read_csv
import pandas as pd
= pd.read_csv("countries.csv")
df df.head()
country | continent | life_expectancy | population | gdp | |
---|---|---|---|---|---|
0 | Afghanistan | Asia | 54.863 | 22856302 | 15153728226 |
1 | Albania | Europe | 74.200 | 3071856 | 12886435920 |
2 | Algeria | Africa | 68.963 | 30533827 | 155661450046 |
3 | Angola | Africa | 45.234 | 13926373 | 34063908358 |
4 | Antigua and Barbuda | N. America | 73.544 | 77656 | 989182128 |