Pandas DataFrame Practice Questions with Solutions

A Pandas DataFrame is a two-dimensional data structure that stores data in rows and columns, similar to an Excel spreadsheet or SQL table. It is one of the most powerful features of the Pandas library and is widely used for data cleaning, analysis, transformation, and visualization. A DataFrame can store different data types in each column, making it ideal for handling real-world datasets. In this chapter, you’ll learn how to create, access, modify, and inspect DataFrames through practical coding examples. Pandas DataFrame practice questions with solutions help to understand the concepts.


1. Python Program to Create a DataFrame from a Dictionary

Problem Statement

Write a Python program to create a Pandas DataFrame from a Python dictionary.

Python Solution

import pandas as pd

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

df = pd.DataFrame(student_data)

print(df)

Sample Output

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

Explanation

The pd.DataFrame() function converts a dictionary into a tabular DataFrame where dictionary keys become column names.

Concepts Covered

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

2. 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

Multiple Python lists can be combined into a dictionary and converted into a DataFrame.

Concepts Covered

  • Lists
  • Dictionary
  • DataFrame Creation

3. Python Program to Display the First Five Rows of a DataFrame

Problem Statement

Write a Python program to display the first five rows of a Pandas DataFrame.

Python Solution

import pandas as pd

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

df = pd.DataFrame(data)

print(df.head())

Sample Output

     Name  Marks
0   Rahul     85
1    Aman     90
2   Priya     78
3   Sneha     88
4   Rohit     92

Explanation

The head() function displays the first five rows of a DataFrame. You can also specify the number of rows as an argument.

Concepts Covered

  • head()
  • Data Preview
  • Data Inspection

4. Python Program to Display the Last Three Rows of a DataFrame

Problem Statement

Write a Python program to display the last three rows of a DataFrame.

Python Solution

import pandas as pd

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

df = pd.DataFrame(data)

print(df.tail(3))

Sample Output

     Name  Marks
3   Sneha     88
4   Rohit     92
5  Anjali     95

Explanation

The tail() function displays rows from the bottom of the DataFrame. Passing 3 displays the last three rows.

Concepts Covered

  • tail()
  • Data Preview
  • Data Inspection

5. Python Program to Display the Shape of a DataFrame

Problem Statement

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

Python Solution

import pandas as pd

data = {
    "Name": ["Rahul", "Aman", "Priya", "Sneha"],
    "Age": [20, 21, 19, 22],
    "Marks": [85, 90, 78, 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 represents the number of rows.
  • Second value represents the number of columns.

Concepts Covered

  • shape
  • Rows
  • Columns
  • DataFrame Dimensions

6. Python Program to Display the Column Names of a DataFrame

Problem Statement

Write a Python program to display all column names of a Pandas 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(df.columns)

Sample Output

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

Explanation

The columns attribute returns an Index object containing all column names of the DataFrame.

Concepts Covered

  • columns
  • Column Labels
  • DataFrame Structure

7. Python Program to Display Data Types of All Columns

Problem Statement

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

Python Solution

import pandas as pd

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

df = pd.DataFrame(data)

print(df.dtypes)

Sample Output

Name      object
Age         int64
Marks     float64
dtype: object

Explanation

The dtypes attribute displays the data type of every column present in the DataFrame.

Concepts Covered

  • dtypes
  • Data Types
  • Object
  • Integer
  • Float

8. Python Program to Display Complete Information About a DataFrame

Problem Statement

Write a Python program to display complete information about a DataFrame.

Python Solution

import pandas as pd

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

df = pd.DataFrame(data)

df.info()

Sample Output

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

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

Explanation

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

  • Number of rows
  • Number of columns
  • Non-null values
  • Data types
  • Memory usage

Concepts Covered

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

9. Python Program to Display Statistical Summary of Numerical Columns

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, 90, 78, 88, 95]
}

df = pd.DataFrame(data)

print(df.describe())

Sample Output

             Age      Marks
count   5.000000   5.000000
mean   21.000000  87.200000
std     1.581139   6.300794
min    19.000000  78.000000
25%    20.000000  85.000000
50%    21.000000  88.000000
75%    22.000000  90.000000
max    23.000000  95.000000

Explanation

The describe() function generates statistical information such as count, mean, standard deviation, minimum, quartiles, and maximum values.

Concepts Covered

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

10. Python Program to Select a Single Column from a DataFrame

Problem Statement

Write a Python program to select and display a single column from a Pandas DataFrame.

Python Solution

import pandas as pd

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

df = pd.DataFrame(data)

print(df["Marks"])

Sample Output

0    85
1    90
2    78
Name: Marks, dtype: int64

Explanation

Selecting a column using square brackets returns a Pandas Series containing all values from that column.

Concepts Covered

  • Column Selection
  • Series
  • Square Bracket Notation

11. Python Program to Select Multiple Columns from a DataFrame

Problem Statement

Write a Python program to select multiple columns from a Pandas DataFrame.

Python Solution

import pandas as pd

data = {
    "Name": ["Rahul", "Aman", "Priya", "Sneha"],
    "Age": [20, 21, 19, 22],
    "Marks": [85, 90, 78, 88],
    "City": ["Delhi", "Mumbai", "Jaipur", "Pune"]
}

df = pd.DataFrame(data)

print(df[["Name", "Marks"]])

Sample Output

     Name  Marks
0   Rahul     85
1    Aman     90
2   Priya     78
3   Sneha     88

Explanation

To select multiple columns, pass a list of column names inside double square brackets.

Concepts Covered

  • Multiple Column Selection
  • List of Columns
  • DataFrame Indexing

12. Python Program to Select a Row Using loc[]

Problem Statement

Write a Python program to retrieve a row from a DataFrame using the loc[] method.

Python Solution

import pandas as pd

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

df = pd.DataFrame(data)

print(df.loc[2])

Sample Output

Name     Priya
Age         19
Marks       78
Name: 2, dtype: object

Explanation

The loc[] method selects rows using their index labels. Since the default index starts from 0, loc[2] retrieves the third row.

Concepts Covered

  • loc[]
  • Row Selection
  • Label-Based Indexing

13. Python Program to Select a Row Using iloc[]

Problem Statement

Write a Python program to retrieve a row from a DataFrame using the iloc[] method.

Python Solution

import pandas as pd

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

df = pd.DataFrame(data)

print(df.iloc[1])

Sample Output

Name     Aman
Age        21
Marks      90
Name: 1, dtype: object

Explanation

The iloc[] method selects rows using integer positions instead of labels.

Concepts Covered

  • iloc[]
  • Integer Indexing
  • Row Selection

14. Python Program to Add a New Column to a DataFrame

Problem Statement

Write a Python program to add a new column named Grade to a DataFrame.

Python Solution

import pandas as pd

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

df = pd.DataFrame(data)

df["Grade"] = ["A", "A+", "B"]

print(df)

Sample Output

     Name  Marks Grade
0   Rahul     85     A
1    Aman     90    A+
2   Priya     78     B

Explanation

A new column can be added by assigning values to a new column name.

Concepts Covered

  • Adding Columns
  • Column Assignment
  • DataFrame Modification

15. Python Program to Rename DataFrame Columns

Problem Statement

Write a Python program to rename one or more columns in a DataFrame.

Python Solution

import pandas as pd

data = {
    "Name": ["Rahul", "Aman"],
    "Marks": [85, 90]
}

df = pd.DataFrame(data)

df = df.rename(columns={
    "Name": "Student Name",
    "Marks": "Score"
})

print(df)

Sample Output

  Student Name  Score
0        Rahul     85
1         Aman     90

Explanation

The rename() function changes column names without modifying the original data.

Concepts Covered

  • rename()
  • Renaming Columns
  • DataFrame Modification

11. Python Program to Select Multiple Columns from a DataFrame

Problem Statement

Write a Python program to select multiple columns from a Pandas DataFrame.

Python Solution

import pandas as pd

data = {
    "Name": ["Rahul", "Aman", "Priya", "Sneha"],
    "Age": [20, 21, 19, 22],
    "Marks": [85, 90, 78, 88],
    "City": ["Delhi", "Mumbai", "Jaipur", "Pune"]
}

df = pd.DataFrame(data)

print(df[["Name", "Marks"]])

Sample Output

     Name  Marks
0   Rahul     85
1    Aman     90
2   Priya     78
3   Sneha     88

Explanation

To select multiple columns, pass a list of column names inside double square brackets.

Concepts Covered

  • Multiple Column Selection
  • List of Columns
  • DataFrame Indexing

12. Python Program to Select a Row Using loc[]

Problem Statement

Write a Python program to retrieve a row from a DataFrame using the loc[] method.

Python Solution

import pandas as pd

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

df = pd.DataFrame(data)

print(df.loc[2])

Sample Output

Name     Priya
Age         19
Marks       78
Name: 2, dtype: object

Explanation

The loc[] method selects rows using their index labels. Since the default index starts from 0, loc[2] retrieves the third row.

Concepts Covered

  • loc[]
  • Row Selection
  • Label-Based Indexing

13. Python Program to Select a Row Using iloc[]

Problem Statement

Write a Python program to retrieve a row from a DataFrame using the iloc[] method.

Python Solution

import pandas as pd

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

df = pd.DataFrame(data)

print(df.iloc[1])

Sample Output

Name     Aman
Age        21
Marks      90
Name: 1, dtype: object

Explanation

The iloc[] method selects rows using integer positions instead of labels.

Concepts Covered

  • iloc[]
  • Integer Indexing
  • Row Selection

14. Python Program to Add a New Column to a DataFrame

Problem Statement

Write a Python program to add a new column named Grade to a DataFrame.

Python Solution

import pandas as pd

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

df = pd.DataFrame(data)

df["Grade"] = ["A", "A+", "B"]

print(df)

Sample Output

     Name  Marks Grade
0   Rahul     85     A
1    Aman     90    A+
2   Priya     78     B

Explanation

A new column can be added by assigning values to a new column name.

Concepts Covered

  • Adding Columns
  • Column Assignment
  • DataFrame Modification

15. Python Program to Rename DataFrame Columns

Problem Statement

Write a Python program to rename one or more columns in a DataFrame.

Python Solution

import pandas as pd

data = {
    "Name": ["Rahul", "Aman"],
    "Marks": [85, 90]
}

df = pd.DataFrame(data)

df = df.rename(columns={
    "Name": "Student Name",
    "Marks": "Score"
})

print(df)

Sample Output

  Student Name  Score
0        Rahul     85
1         Aman     90

Explanation

The rename() function changes column names without modifying the original data.

Concepts Covered

  • rename()
  • Renaming Columns
  • DataFrame Modification

next part

16. Python Program to Delete a Column from a DataFrame

Problem Statement

Write a Python program to delete the Age column from a Pandas DataFrame.

Python Solution

import pandas as pd

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

df = pd.DataFrame(data)

df = df.drop(columns=["Age"])

print(df)

Sample Output

    Name  Marks
0  Rahul     85
1   Aman     90
2  Priya     78

Explanation

The drop() function removes one or more columns from a DataFrame. Passing the columns parameter specifies which columns to remove.

Concepts Covered

  • drop()
  • Delete Columns
  • DataFrame Modification

17. Python Program to Filter Rows Based on a Condition

Problem Statement

Write a Python program to display only those students whose marks are greater than or equal to 85.

Python Solution

import pandas as pd

data = {
    "Name": ["Rahul", "Aman", "Priya", "Sneha", "Rohit"],
    "Marks": [85, 90, 78, 88, 82]
}

df = pd.DataFrame(data)

filtered_df = df[df["Marks"] >= 85]

print(filtered_df)

Sample Output

     Name  Marks
0   Rahul     85
1    Aman     90
3   Sneha     88

Explanation

Boolean indexing filters rows based on a condition. Only the rows satisfying the condition are returned.

Concepts Covered

  • Boolean Indexing
  • Conditional Filtering
  • DataFrame Selection

Chapter Summary

In this chapter, you learned how to work with Pandas DataFrames, including creating DataFrames from dictionaries and lists, viewing data using head() and tail(), inspecting dataset information, selecting rows and columns, adding, renaming, and deleting columns, and filtering rows using conditions. These operations form the foundation of data manipulation and are essential for real-world data analysis projects.


Key Takeaways

  • A DataFrame is a two-dimensional tabular data structure in Pandas.
  • DataFrames can be created from dictionaries, lists, CSV files, Excel files, and other data sources.
  • Use head(), tail(), info(), and describe() to quickly inspect datasets.
  • Select rows using loc[] and iloc[].
  • Select one or multiple columns using square bracket notation.
  • Columns can be added, renamed, and deleted easily.
  • Boolean indexing helps filter data efficiently based on conditions.

Frequently Asked Questions (FAQs)

1. What is a Pandas DataFrame?

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


2. How do you create a DataFrame in Pandas?

Use the pd.DataFrame() function.

import pandas as pd

data = {
    "Name": ["Rahul", "Aman"],
    "Marks": [85, 90]
}

df = pd.DataFrame(data)
print(df)

3. What is the difference between loc[] and iloc[]?

  • loc[] selects data using row labels (indexes).
  • iloc[] selects data using integer positions.

4. How do you display the first five rows of a DataFrame?

Use the head() function.

print(df.head())

5. How do you rename a column in Pandas?

Use the rename() function.

df.rename(columns={"Marks": "Score"}, inplace=True)

6. How do you delete a column from a DataFrame?

Use the drop() function.

df = df.drop(columns=["Age"])

7. How do you filter rows based on a condition?

Use Boolean indexing.

filtered_df = df[df["Marks"] > 80]

print(filtered_df)

8. Why are DataFrames important in Data Analysis?

DataFrames make it easy to clean, organize, analyze, transform, and visualize structured data. They are one of the most widely used data structures in data science, machine learning, business analytics, and data engineering.

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

Scroll to Top