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

Program 7: Read a CSV File and Display Its Information

Read the CSV file saved in your system and display its information.

1Aim

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

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

2Program

csv_info.py
# Program 7: Read a CSV file saved in your system and display its information

import pandas as pd

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

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

print("Rows and columns :", data.shape)
print()
print("Information about the data:")
data.info()
print()
print("Missing values in each column:")
print(data.isnull().sum())
print()
print("Summary of the number columns:")
print(data.describe().round(1))

3Output

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

Output
Rows and columns : (344, 8)

Information about the data:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 344 entries, 0 to 343
Data columns (total 8 columns):
 #   Column             Non-Null Count  Dtype  
---  ------             --------------  -----  
 0   species            344 non-null    object 
 1   island             344 non-null    object 
 2   bill_length_mm     342 non-null    float64
 3   bill_depth_mm      342 non-null    float64
 4   flipper_length_mm  342 non-null    float64
 5   body_mass_g        342 non-null    float64
 6   sex                333 non-null    object 
 7   year               344 non-null    int64  
dtypes: float64(4), int64(1), object(3)
memory usage: 21.6+ KB

Missing values in each column:
species               0
island                0
bill_length_mm        2
bill_depth_mm         2
flipper_length_mm     2
body_mass_g           2
sex                  11
year                  0
dtype: int64

Summary of the number columns:
       bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g    year
count           342.0          342.0              342.0        342.0   344.0
mean             43.9           17.2              200.9       4201.8  2008.0
std               5.5            2.0               14.1        802.0     0.8
min              32.1           13.1              172.0       2700.0  2007.0
25%              39.2           15.6              190.0       3550.0  2007.0
50%              44.4           17.3              197.0       4050.0  2008.0
75%              48.5           18.7              213.0       4750.0  2009.0
max              59.6           21.5              231.0       6300.0  2009.0

4How it works

data.shape

The number of rows and columns, as (rows, columns).

data.info()

One line per column: its name, how many values are not missing, and its data type. info() prints by itself, so it is not put inside print().

data.isnull().sum()

Counts the missing values in each column.

data.describe()

Count, mean, standard deviation, minimum, quartiles and maximum for every number column.

5Try this

  • Print data.columns to list the column names.
  • Print data["species"].value_counts().
  • Find the average body mass of each species with groupby().

6Viva questions

▸What does info() show?

Each column's name, its count of non-missing values and its data type, plus the memory used.

▸What is the difference between info() and describe()?

info() describes the columns; describe() gives statistics such as mean and standard deviation for the number columns.

▸What does dtype object mean?

The column holds text.

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.