Pandas Introduction and Installation Practice Questions with Solutions

Pandas is one of the most popular Python libraries for data analysis and data manipulation. It provides powerful data structures such as Series and DataFrame, making it easy to organize, clean, analyze, and visualize structured data. Whether you are working with CSV files, Excel sheets, SQL databases, or JSON data, Pandas simplifies complex data operations with just a few lines of code.

In this chapter, you will learn how to install Pandas, import it into your Python program, verify the installation, and create your first Series and DataFrame. These practice questions are designed to help beginners build a strong foundation before moving on to advanced data analysis topics. Pandas Introduction and Installation practice questions with solutions help to build concepts.


1. Python Program to Install Pandas Using pip

Problem Statement

Write a Python command to install the Pandas library using pip and verify that it has been installed successfully.

Python Solution

Install Pandas

pip install pandas

Verify Installation

import pandas as pd

print("Pandas Version:", pd.__version__)
print("Pandas Installed Successfully!")

Sample Output

Pandas Version: 2.3.1
Pandas Installed Successfully!

Note: The version number may differ depending on your installed version.

Explanation

The pip install pandas command downloads and installs the latest stable version of the Pandas library. After installation, pd.__version__ displays the installed version to confirm that Pandas is available.

Concepts Covered

  • pip install
  • Importing Pandas
  • Checking Installed Version

2. Python Program to Import Pandas and Create Your First Series

Problem Statement

Write a Python program to create a Pandas Series containing the marks of five students.

Python Solution

import pandas as pd

marks = pd.Series([78, 85, 92, 88, 76])

print("Student Marks:")
print(marks)

Sample Output

Student Marks:

0    78
1    85
2    92
3    88
4    76
dtype: int64

Explanation

A Series is a one-dimensional labeled data structure in Pandas. If no index is specified, Pandas automatically assigns integer indexes starting from 0.

Concepts Covered

  • pd.Series()
  • Default Index
  • One-Dimensional Data

3. Python Program to Create a Pandas Series with Custom Index

Problem Statement

Write a Python program to create a Pandas Series using custom index labels.

Python Solution

import pandas as pd

students = pd.Series(
    [90, 85, 88, 91],
    index=["Rahul", "Aman", "Priya", "Neha"]
)

print(students)

Sample Output

Rahul    90
Aman     85
Priya    88
Neha     91
dtype: int64

Explanation

Instead of default numeric indexes, custom labels make the data more meaningful and easier to access.

Concepts Covered

  • Custom Index
  • Series Labels
  • pd.Series()

4. Python Program to Create Your First DataFrame

Problem Statement

Write a Python program to create a Pandas DataFrame containing student names, ages, and marks.

Python Solution

import pandas as pd

student_data = {
    "Name": ["Rahul", "Aman", "Priya", "Sneha"],
    "Age": [20, 21, 19, 22],
    "Marks": [85, 78, 92, 88]
}

df = pd.DataFrame(student_data)

print(df)

Sample Output

     Name  Age  Marks
0   Rahul   20     85
1    Aman   21     78
2   Priya   19     92
3   Sneha   22     88

Explanation

A DataFrame is a two-dimensional data structure in Pandas that stores data in rows and columns, similar to an Excel spreadsheet or SQL table.

Concepts Covered

  • pd.DataFrame()
  • Dictionary to DataFrame
  • Rows and Columns

5. Python Program to Check the Installed Pandas Version

Problem Statement

Write a Python program to display the currently installed version of the Pandas library.

Python Solution

import pandas as pd

print("Installed Pandas Version:")
print(pd.__version__)

Sample Output

Installed Pandas Version:
2.3.1

Note: The version number may vary depending on your installed Pandas version.

Explanation

The __version__ attribute returns the version number of the installed Pandas package. It is useful for checking compatibility before running a project.

Concepts Covered

  • pd.__version__
  • Pandas Version
  • Package Information

6. Python Program to Create a DataFrame from Multiple Lists

Problem Statement

Write a Python program to create a Pandas DataFrame using multiple Python lists.

Python Solution

import pandas as pd

names = ["Rahul", "Aman", "Priya", "Sneha"]
ages = [20, 21, 19, 22]
courses = ["Python", "Java", "Data Science", "Web Development"]

df = pd.DataFrame({
    "Name": names,
    "Age": ages,
    "Course": courses
})

print(df)

Sample Output

     Name  Age            Course
0   Rahul   20            Python
1    Aman   21              Java
2   Priya   19      Data Science
3   Sneha   22   Web Development

Explanation

A DataFrame can also be created by combining multiple Python lists into a dictionary, where each key becomes a column name.

Concepts Covered

  • DataFrame
  • Lists
  • Dictionary
  • Multiple Columns

7. Python Program to Display the First and Last Rows of a DataFrame

Problem Statement

Write a Python program to display the first three rows and the last two rows of a Pandas DataFrame.

Python Solution

import pandas as pd

data = {
    "Name": ["Rahul", "Aman", "Priya", "Sneha", "Rohit", "Anjali"],
    "Marks": [85, 78, 92, 88, 81, 95]
}

df = pd.DataFrame(data)

print("First Three Rows:")
print(df.head(3))

print("\nLast Two Rows:")
print(df.tail(2))

Sample Output

First Three Rows:
     Name  Marks
0   Rahul     85
1    Aman     78
2   Priya     92

Last Two Rows:
     Name  Marks
4   Rohit     81
5  Anjali     95

Explanation

  • head(n) displays the first n rows.
  • tail(n) displays the last n rows.

These functions are commonly used to quickly inspect datasets.

Concepts Covered

  • head()
  • tail()
  • Data Inspection

8. Python Program to Display Basic Information About a DataFrame

Problem Statement

Write a Python program to display basic information about a DataFrame, including the number of rows, columns, data types, and memory usage.

Python Solution

import pandas as pd

students = {
    "Name": ["Rahul", "Aman", "Priya", "Sneha"],
    "Age": [20, 21, 19, 22],
    "Marks": [85, 78, 92, 88]
}

df = pd.DataFrame(students)

print("DataFrame Information:\n")

df.info()

Sample Output

DataFrame Information:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   Name    4 non-null      object
 1   Age     4 non-null      int64
 2   Marks   4 non-null      int64

dtypes: int64(2), object(1)
memory usage: 228.0+ bytes

Explanation

The info() method provides a quick summary of the DataFrame, including:

  • Total number of rows
  • Total number of columns
  • Non-null values
  • Data types of each column
  • Memory usage

It is one of the most frequently used methods in data analysis.

Concepts Covered

  • info()
  • DataFrame Summary
  • Data Types
  • Memory Usage

9. Python Program to Read Data from a CSV File

Problem Statement

Write a Python program to read student data from a CSV file using Pandas and display its contents.

Python Solution

import pandas as pd

df = pd.read_csv("students.csv")

print("Student Data:")
print(df)

Sample Output

Student Data:

   ID    Name  Age  Marks
0  101   Rahul   20     85
1  102    Aman   21     78
2  103   Priya   19     92
3  104   Sneha   22     88

Explanation

The read_csv() function imports data from a CSV file and stores it in a DataFrame, making it easy to analyze and manipulate.

Concepts Covered

  • pd.read_csv()
  • CSV File
  • Data Import
  • DataFrame

10. Python Program to Display the Shape of a DataFrame

Problem Statement

Write a Python program to display the number of rows and columns present in a DataFrame.

Python Solution

import pandas as pd

data = {
    "Name": ["Rahul", "Aman", "Priya", "Sneha"],
    "Age": [20, 21, 19, 22],
    "Marks": [85, 78, 92, 88]
}

df = pd.DataFrame(data)

print("Shape of DataFrame:")
print(df.shape)

Sample Output

Shape of DataFrame:
(4, 3)

Explanation

The shape attribute returns a tuple where:

  • First value = Number of rows
  • Second value = Number of columns

Concepts Covered

  • shape
  • Rows
  • Columns
  • DataFrame Dimensions

11. Python Program to Display Column Names

Problem Statement

Write a Python program to display all column names of a DataFrame.

Python Solution

import pandas as pd

data = {
    "Name": ["Rahul", "Aman", "Priya"],
    "Age": [20, 21, 19],
    "Course": ["Python", "Java", "Data Science"]
}

df = pd.DataFrame(data)

print("Column Names:")
print(df.columns)

Sample Output

Column Names:
Index(['Name', 'Age', 'Course'], dtype='object')

Explanation

The columns attribute returns the names of all columns in the DataFrame.

Concepts Covered

  • columns
  • Column Labels
  • DataFrame Structure

12. Python Program to Display Data Types of Each Column

Problem Statement

Write a Python program to display the data type of every column in a DataFrame.

Python Solution

import pandas as pd

data = {
    "Name": ["Rahul", "Aman", "Priya"],
    "Age": [20, 21, 19],
    "Marks": [85.5, 78.0, 92.5]
}

df = pd.DataFrame(data)

print("Column Data Types:")
print(df.dtypes)

Sample Output

Column Data Types:

Name      object
Age        int64
Marks    float64
dtype: object

Explanation

The dtypes attribute displays the data type of each column, helping you understand the structure of your dataset before performing analysis.

Concepts Covered

  • dtypes
  • Data Types
  • Integer
  • Float
  • Object

13. Python Program to Display Statistical Summary of a DataFrame

Problem Statement

Write a Python program to display the statistical summary of all numerical columns in a DataFrame.

Python Solution

import pandas as pd

data = {
    "Age": [20, 21, 19, 22, 23],
    "Marks": [85, 78, 92, 88, 95]
}

df = pd.DataFrame(data)

print(df.describe())

Sample Output

             Age      Marks
count   5.000000   5.000000
mean   21.000000  87.600000
std     1.581139   6.580274
min    19.000000  78.000000
25%    20.000000  85.000000
50%    21.000000  88.000000
75%    22.000000  92.000000
max    23.000000  95.000000

Explanation

The describe() function generates a statistical summary of numerical columns, including:

  • Count
  • Mean
  • Standard Deviation
  • Minimum Value
  • Quartiles (25%, 50%, 75%)
  • Maximum Value

It is one of the most commonly used functions in data analysis.

Concepts Covered

  • describe()
  • Statistical Summary
  • Mean
  • Standard Deviation
  • Quartiles
  • Maximum
  • Minimum

14. Python Program to Check Missing Values in a DataFrame

Problem Statement

Write a Python program to identify missing values in a DataFrame using Pandas.

Python Solution

import pandas as pd
import numpy as np

data = {
    "Name": ["Rahul", "Aman", "Priya", "Sneha"],
    "Age": [20, np.nan, 19, 22],
    "Marks": [85, 78, np.nan, 88]
}

df = pd.DataFrame(data)

print("Missing Values:")
print(df.isnull())

Sample Output

Missing Values:

    Name    Age  Marks
0  False  False  False
1  False   True  False
2  False  False   True
3  False  False  False

Explanation

The isnull() function returns a DataFrame containing Boolean values.

  • True → Missing value
  • False → Value exists

It is commonly used before cleaning a dataset.

Concepts Covered

  • isnull()
  • Missing Values
  • Data Cleaning

15. Python Program to Display the Total Number of Missing Values

Problem Statement

Write a Python program to count the total missing values in each column.

Python Solution

import pandas as pd
import numpy as np

data = {
    "Name": ["Rahul", "Aman", "Priya", "Sneha"],
    "Age": [20, np.nan, 19, np.nan],
    "Marks": [85, 78, np.nan, 88]
}

df = pd.DataFrame(data)

print(df.isnull().sum())

Sample Output

Name     0
Age      2
Marks    1
dtype: int64

Explanation

  • isnull() identifies missing values.
  • sum() counts the number of True values in each column.

This helps determine which columns require data cleaning.

Concepts Covered

  • isnull()
  • sum()
  • Missing Value Count

16. Python Program to Export a DataFrame to a CSV File

Problem Statement

Write a Python program to save a DataFrame as a CSV file.

Python Solution

import pandas as pd

data = {
    "Name": ["Rahul", "Aman", "Priya"],
    "Marks": [85, 78, 92]
}

df = pd.DataFrame(data)

df.to_csv("students.csv", index=False)

print("CSV file exported successfully.")

Sample Output

CSV file exported successfully.

Explanation

The to_csv() function exports the DataFrame into a CSV file.

Setting index=False prevents Pandas from saving row indexes in the file.

Concepts Covered

  • to_csv()
  • CSV Export
  • File Handling

17. Python Program to Export a DataFrame to an Excel File

Problem Statement

Write a Python program to save a DataFrame as an Excel file.

Python Solution

import pandas as pd

data = {
    "Name": ["Rahul", "Aman", "Priya"],
    "Age": [20, 21, 19],
    "Marks": [85, 78, 92]
}

df = pd.DataFrame(data)

df.to_excel("students.xlsx", index=False)

print("Excel file exported successfully.")

Sample Output

Excel file exported successfully.

Explanation

The to_excel() function exports the DataFrame to an Excel workbook.

Note: You may need to install the openpyxl package.

pip install openpyxl

Concepts Covered

  • to_excel()
  • Excel Export
  • openpyxl

Chapter Summary

In this chapter, you learned the fundamentals of Pandas, including installation, importing the library, creating Series and DataFrames, reading CSV files, inspecting datasets, checking data types, viewing statistical summaries, identifying missing values, and exporting data to CSV and Excel files. These concepts form the foundation for performing data analysis and data manipulation using Pandas.


Key Takeaways

  • Pandas is the most widely used Python library for data analysis.
  • A Series is a one-dimensional labeled data structure.
  • A DataFrame is a two-dimensional tabular data structure.
  • Pandas can import data from CSV, Excel, JSON, SQL, and many other sources.
  • Functions like head(), tail(), info(), and describe() help inspect datasets quickly.
  • Missing values can be detected using isnull().
  • DataFrames can be exported easily using to_csv() and to_excel().

Frequently Asked Questions (FAQs)

1. What is Pandas in Python?

Pandas is an open-source Python library used for data manipulation, data cleaning, and data analysis. It provides powerful data structures such as Series and DataFrame.


2. Why is Pandas used in Data Analytics?

Pandas simplifies tasks such as reading files, filtering records, cleaning data, grouping data, and performing statistical analysis, making it an essential library for data analytics.


3. What is the difference between a Series and a DataFrame?

A Series is a one-dimensional labeled array, whereas a DataFrame is a two-dimensional table with rows and columns.


4. How do I install Pandas?

You can install Pandas using pip:

pip install pandas

5. Which file formats can Pandas read?

Pandas can read various file formats, including:

  • CSV
  • Excel
  • JSON
  • SQL Databases
  • HTML Tables
  • XML
  • Parquet

6. Which function is used to read a CSV file?

The read_csv() function is used to import CSV files into a Pandas DataFrame.

Example:

import pandas as pd

df = pd.read_csv("students.csv")

7. Is Pandas required for Machine Learning?

Yes. Pandas is widely used in machine learning for data preprocessing, cleaning, feature engineering, and preparing datasets before training machine learning models.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top