Pandas Exercises for Data Analysis (Interactive)

A comprehensive set of Pandas exercises designed to strengthen data manipulation and analysis skills in Python, covering everything from basic operations to advanced data transformation techniques.
machine learning + Build Your First AI App with Python — Step-by-Step Tutorial
This is an interactive version of the popular 101 Pandas Exercises. You can edit and run every code block directly in your browser — no installation needed. All code runs locally in your browser and nothing is sent to any server.
Click ‘Run’ or press Ctrl+Enter on any code block to execute it. The first run may take a few seconds to initialize.
1. How to import pandas and check the version?
Import pandas and check the version used.
Input:
Task: Import pandas and check the version # Write your code below
Desired Output:
Your pandas version will be shown here # e.g., 2.1.0
2. How to create a series from a list, numpy array and dict?
Create a pandas series from each of the items below: a list, numpy and a dictionary
Input:
Task: Create a series from a list, numpy array and dict import pandas as pd import numpy as np mylist = list('abcedfghijklmnopqrstuvwxyz') myarr = np.arange(26) mydict = dict(zip(mylist, myarr)) # Write your code below
Desired Output:
a 0 b 1 c 2 d 4 e 3 dtype: int64
3. How to convert the index of a series into a column of a dataframe?
Difficulty Level: L1
Convert the series ser into a dataframe with its index as another column on the dataframe.
Input:
Task: Convert the index of a series into a column of a dataframe import numpy as np import pandas as pd mylist = list('abcedfghijklmnopqrstuvwxyz') myarr = np.arange(26) mydict = dict(zip(mylist, myarr)) ser = pd.Series(mydict) # Write your code below
Desired Output:
index 0 0 a 0 1 b 1 2 c 2 3 d 4 4 e 3
4. How to combine many series to form a dataframe?
Difficulty Level: L1
Combine ser1 and ser2 to form a dataframe.
Input:
Task: Combine many series to form a dataframe import pandas as pd import numpy as np ser1 = pd.Series(list('abcedfghijklmnopqrstuvwxyz')) ser2 = pd.Series(np.arange(26)) # Write your code below
Desired Output:
col1 col2 0 a 0 1 b 1 2 c 2 3 e 3 4 d 4
5. How to assign name to the series’ index?
Difficulty Level: L1
Give a name to the series ser calling it ‘alphabets’.
Input:
Task: Assign name to the series' index import numpy as np import pandas as pd ser = pd.Series(list('abcedfghijklmnopqrstuvwxyz')) # Write your code below
Desired Output:
0 a 1 b 2 c 3 e 4 d Name: alphabets, dtype: object
6. How to get the items of series A not present in series B?
Difficulty Level: L2
From ser1 remove items present in ser2.
Input:
Task: Get the items of series A not present in series B import numpy as np import pandas as pd ser1 = pd.Series([1, 2, 3, 4, 5]) ser2 = pd.Series([4, 5, 6, 7, 8]) # Write your code below
Desired Output:
0 1 1 2 2 3 dtype: int64
7. How to get the items not common to both series A and series B?
Difficulty Level: L2
Get all items of ser1 and ser2 not common to both.
Input:
Task: Get the items not common to both series A and series B import numpy as np import pandas as pd ser1 = pd.Series([1, 2, 3, 4, 5]) ser2 = pd.Series([4, 5, 6, 7, 8]) # Write your code below
Desired Output:
0 1 1 2 2 3 5 6 6 7 7 8 dtype: int64
8. How to get the minimum, 25th percentile, median, 75th, and max of a numeric series?
Difficuty Level: L2 Compute the minimum, 25th percentile, median, 75th, and maximum of ser.
Input:
Task: Get the minimum, 25th percentile, median, 75th, and max of a numeric series import numpy as np import pandas as pd ser = pd.Series(np.random.normal(10, 5, 25)) # Write your code below
Desired Output:
array([ -1.39, 6.49, 10.26, 13.07, 25.81]) # (exact values vary — output shows the 5 percentiles)
9. How to get frequency counts of unique items of a series?
Difficulty Level: L1
Calculte the frequency counts of each unique value ser.
Input:
Task: Get frequency counts of unique items of a series import numpy as np import pandas as pd ser = pd.Series(np.take(list('abcdefgh'), np.random.randint(8, size=30))) # Write your code below
Desired Output:
Output shows value_counts, e.g.: b 6 a 5 c 4 ... dtype: int64
10. How to keep only top 2 most frequent values as it is and replace everything else as ‘Other’?
Difficulty Level: L2
From ser, keep the top 2 most frequent items as it is and replace everything else as ‘Other’.
Input:
Task: Keep only top 2 most frequent values as it is and replace everything else as 'Other' import numpy as np import pandas as pd np.random.seed(100) ser = pd.Series(np.random.randint(1, 5, [12])) # Write your code below
Desired Output:
Top 2 Freq: 4 5 3 3 ... 0 Other 1 Other 2 3 ...
11. How to bin a numeric series to 10 groups of equal size?
Difficulty Level: L2
Bin the series ser into 10 equal deciles and replace the values with the bin name.
Input:
Task: Bin a numeric series to 10 groups of equal size import numpy as np import pandas as pd ser = pd.Series(np.random.random(20)) # Write your code below
Desired Output:
0 0.556912 1 0.892955 ... dtype: float64 0 7th 1 9th ... dtype: category Categories (10, object): [1st < 2nd < 3rd < ... < 9th < 10th]
12. How to convert a numpy array to a dataframe of given shape? (L1)
Difficulty Level: L1
Reshape the series ser into a dataframe with 7 rows and 5 columns
Input:
Task: Convert a numpy array to a dataframe of given shape? (L1) import numpy as np import pandas as pd ser = pd.Series(np.random.randint(1, 10, 35)) # Write your code below
Desired Output:
0 1 2 3 4 0 1 2 1 2 5 1 1 2 4 5 2 2 1 3 3 2 8 3 8 6 4 9 6 4 2 1 1 8 5 5 3 2 8 5 6 6 1 5 5 4 6 # (values vary — output is a 7x5 DataFrame)
13. How to find the positions of numbers that are multiples of 3 from a series?
Difficulty Level: L2
Find the positions of numbers that are multiples of 3 from ser.
Input:
Task: Find the positions of numbers that are multiples of 3 from a series import numpy as np import pandas as pd ser = pd.Series(np.random.randint(1, 10, 7)) # Write your code below
Desired Output:
0 6 1 8 ... dtype: int64 array([[0], [2], [4]]) # (positions where values are multiples of 3)
14. How to extract items at given positions from a series
Difficulty Level: L1
From ser, extract the items at positions in list pos.
Input:
Task: Extract items at given positions from a series import numpy as np import pandas as pd ser = pd.Series(list('abcdefghijklmnopqrstuvwxyz')) pos = [0, 4, 8, 14, 20] # Write your code below
Desired Output:
0 a 4 e 8 i 14 o 20 u dtype: object
15. How to stack two series vertically and horizontally ?
Difficulty Level: L1
Stack ser1 and ser2 vertically and horizontally (to form a dataframe).
Input:
Task: Stack two series vertically and horizontally import numpy as np import pandas as pd ser1 = pd.Series(range(5)) ser2 = pd.Series(list('abcde')) # Write your code below
Desired Output:
0 1 0 0 a 1 1 b 2 2 c 3 3 d 4 4 e
16. How to get the positions of items of series A in another series B?
Difficulty Level: L2
Get the positions of items of ser2 in ser1 as a list.
Input:
Task: Get the positions of items of series A in another series B import numpy as np import pandas as pd ser1 = pd.Series([10, 9, 6, 5, 3, 1, 12, 8, 13]) ser2 = pd.Series([1, 3, 10, 13]) # Write your code below
Desired Output:
[5, 4, 0, 8]
17. How to compute the mean squared error on a truth and predicted series?
Difficulty Level: L2
Compute the mean squared error of truth and pred series.
Input:
Task: Compute the mean squared error on a truth and predicted series import numpy as np import pandas as pd truth = pd.Series(range(10)) pred = pd.Series(range(10)) + np.random.random(10) # Write your code below
18. How to convert the first character of each element in a series to uppercase?
Difficulty Level: L2
Change the first character of each word to upper case in each word of ser.
**Inpu
Source: Hacker News














