LambdaLabTM
Artificial Intelligence · Class 10 · Practical File · Program 6 of 15
Data with pandasFrom the CBSE curriculum list⏱️ 6 min read

Program 6: Read a CSV File and Display 10 Rows

Read the CSV file saved in your system and display 10 rows.

1Aim

Read the CSV file saved in your system and display 10 rows.
Python 3pandas⬇ penguins.csv

Keep penguins.csv in the same folder as the program.

2Program

csv_rows.py
# Program 6: Read a CSV file saved in your system and display 10 rows
# penguins.csv is the Palmer Penguins dataset used in the Statistical
# Data unit. Keep it in the same folder as this program.

import pandas as pd

# Show every column instead of "..." in the middle of wide tables.
pd.set_option("display.max_columns", None)
pd.set_option("display.width", 120)

data = pd.read_csv("penguins.csv")

print("The first 10 rows:")
print(data.head(10))
print()
print("The last 3 rows:")
print(data.tail(3))

3Output

The output of a real run, exactly as it appeared.

Output
The first 10 rows:
  species     island  bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g     sex  year
0  Adelie  Torgersen            39.1           18.7              181.0       3750.0    male  2007
1  Adelie  Torgersen            39.5           17.4              186.0       3800.0  female  2007
2  Adelie  Torgersen            40.3           18.0              195.0       3250.0  female  2007
3  Adelie  Torgersen             NaN            NaN                NaN          NaN     NaN  2007
4  Adelie  Torgersen            36.7           19.3              193.0       3450.0  female  2007
5  Adelie  Torgersen            39.3           20.6              190.0       3650.0    male  2007
6  Adelie  Torgersen            38.9           17.8              181.0       3625.0  female  2007
7  Adelie  Torgersen            39.2           19.6              195.0       4675.0    male  2007
8  Adelie  Torgersen            34.1           18.1              193.0       3475.0     NaN  2007
9  Adelie  Torgersen            42.0           20.2              190.0       4250.0     NaN  2007

The last 3 rows:
       species island  bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g     sex  year
341  Chinstrap  Dream            49.6           18.2              193.0       3775.0    male  2009
342  Chinstrap  Dream            50.8           19.0              210.0       4100.0    male  2009
343  Chinstrap  Dream            50.2           18.7              198.0       3775.0  female  2009

4How it works

import pandas as pd

pandas is the library for working with tables of data.

pd.read_csv("penguins.csv")

Reads the CSV file into a DataFrame — a table with rows and columns. The file must be in the same folder as the program, or give its full path.

pd.set_option("display.max_columns", None)

Without this, pandas hides the middle columns of a wide table behind "...".

data.head(10)

The first 10 rows. data.tail(3) gives the last 3.

Note
penguins.csv is the real Palmer Penguins dataset (CC0 licence, Gorman et al. 2014) — the same data the Statistical Data unit uses in Orange.

5Try this

  • Display the first 5 rows (head() with no number).
  • Display only the species and island columns: data[["species", "island"]].head(10)
  • Make your own CSV in a spreadsheet and read it.

6Viva questions

▸What does CSV stand for?

Comma Separated Values.

▸Which function reads a CSV file in pandas?

pd.read_csv().

▸What does head(10) do?

It returns the first 10 rows of the DataFrame.

▸What does NaN in the output mean?

A missing value — that cell was empty in the file.

Tip
In your practical file, write the aim, the program and the output for each program — run it yourself and copy your own output. See The Practical File for how to set the file out.