101 Pandas Exercises for Data Analysis (Interactive)
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.
By Prabhakaran S · March 2026
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:
PyInteractive Python
# 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
Show Solution
PyInteractive Python
import numpy as np # optionalimport pandas as pdprint(pd.__version__)print(pd.show_versions())
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:
PyInteractive Python
# Task: Create a series from a list, numpy array and dictimport pandas as pdimport numpy as npmylist =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
Show Solution
PyInteractive Python
import pandas as pd# Inputsimport numpy as npmylist =list('abcedfghijklmnopqrstuvwxyz')myarr = np.arange(26)mydict =dict(zip(mylist, myarr))# Solutionser1 = pd.Series(mylist)ser2 = pd.Series(myarr)ser3 = pd.Series(mydict)print(ser3.head())
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:
PyInteractive Python
# Task: Convert the index of a series into a column of a dataframeimport numpy as npimport pandas as pdmylist =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
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputmylist =list('abcedfghijklmnopqrstuvwxyz')myarr = np.arange(26)mydict =dict(zip(mylist, myarr))ser = pd.Series(mydict)# Solutiondf = ser.to_frame().reset_index()print(df.head())
4. How to combine many series to form a dataframe?
Difficulty Level: L1
Combine ser1 and ser2 to form a dataframe.
Input:
PyInteractive Python
# Task: Combine many series to form a dataframeimport pandas as pdimport numpy as npser1 = pd.Series(list('abcedfghijklmnopqrstuvwxyz'))ser2 = pd.Series(np.arange(26))# Write your code below
Give a name to the series ser calling it ‘alphabets’.
Input:
PyInteractive Python
# Task: Assign name to the series' indeximport numpy as npimport pandas as pdser = pd.Series(list('abcedfghijklmnopqrstuvwxyz'))# Write your code below
Desired Output:
0 a
1 b
2 c
3 e
4 d
Name: alphabets, dtype: object
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputser = pd.Series(list('abcedfghijklmnopqrstuvwxyz'))# Solutionser.name ='alphabets'print(ser.head())
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:
PyInteractive Python
# Task: Get the items of series A not present in series Bimport numpy as npimport pandas as pdser1 = pd.Series([1, 2, 3, 4, 5])ser2 = pd.Series([4, 5, 6, 7, 8])# Write your code below
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:
PyInteractive Python
# Task: Get the items not common to both series A and series Bimport numpy as npimport pandas as pdser1 = pd.Series([1, 2, 3, 4, 5])ser2 = pd.Series([4, 5, 6, 7, 8])# Write your code below
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:
PyInteractive Python
# Task: Get the minimum, 25th percentile, median, 75th, and max of a numeric seriesimport numpy as npimport pandas as pdser = pd.Series(np.random.normal(10, 5, 25))# Write your code below
import numpy as npimport pandas as pd# Inputser = pd.Series(np.random.normal(10, 5, 25))# Solutionprint(np.percentile(ser, q=[0, 25, 50, 75, 100]))
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:
PyInteractive Python
# Task: Get frequency counts of unique items of a seriesimport numpy as npimport pandas as pdser = 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
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputser = pd.Series(np.take(list('abcdefgh'), np.random.randint(8, size=30)))# Solutionprint(ser.value_counts())
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:
PyInteractive Python
# Task: Keep only top 2 most frequent values as it is and replace everything else as 'Other'import numpy as npimport pandas as pdnp.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
...
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputnp.random.seed(100)ser = pd.Series(np.random.randint(1, 5, [12]))# Solutionprint("Top 2 Freq:", ser.value_counts())ser[~ser.isin(ser.value_counts().index[:2])] ='Other'print(ser)
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:
PyInteractive Python
# Task: Bin a numeric series to 10 groups of equal sizeimport numpy as npimport pandas as pdser = pd.Series(np.random.random(20))# Write your code below
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:
PyInteractive Python
# Task: Convert a numpy array to a dataframe of given shape? (L1)import numpy as npimport pandas as pdser = pd.Series(np.random.randint(1, 10, 35))# Write your code below
import numpy as npimport pandas as pd# Inputser = pd.Series(np.random.randint(1, 10, 35))# Solutiondf = pd.DataFrame(ser.values.reshape(7,5))print(df)
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:
PyInteractive Python
# Task: Find the positions of numbers that are multiples of 3 from a seriesimport numpy as npimport pandas as pdser = pd.Series(np.random.randint(1, 10, 7))# Write your code below
Desired Output:
0618...
dtype: int64
array([[0],
[2],
[4]])
# (positions where values are multiples of 3)
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputser = pd.Series(np.random.randint(1, 10, 7))# Solutionprint(ser)print(np.argwhere(ser %3==0))
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:
PyInteractive Python
# Task: Extract items at given positions from a seriesimport numpy as npimport pandas as pdser = 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
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputser = pd.Series(list('abcdefghijklmnopqrstuvwxyz'))pos = [0, 4, 8, 14, 20]# Solutionprint(ser.take(pos))
15. How to stack two series vertically and horizontally ?
Difficulty Level: L1
Stack ser1 and ser2 vertically and horizontally (to form a dataframe).
Input:
PyInteractive Python
# Task: Stack two series vertically and horizontallyimport numpy as npimport pandas as pdser1 = 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
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputser1 = pd.Series(range(5))ser2 = pd.Series(list('abcde'))# Output# Verticalprint(pd.concat([ser1, ser2]))# Horizontaldf = pd.concat([ser1, ser2], axis=1)print(df)
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:
PyInteractive Python
# Task: Get the positions of items of series A in another series Bimport numpy as npimport pandas as pdser1 = 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]
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputser1 = pd.Series([10, 9, 6, 5, 3, 1, 12, 8, 13])ser2 = pd.Series([1, 3, 10, 13])# Solution 1[np.where(i == ser1)[0].tolist()[0] for i in ser2]# Solution 2print([pd.Index(ser1).get_loc(i) for i in ser2])
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:
PyInteractive Python
# Task: Compute the mean squared error on a truth and predicted seriesimport numpy as npimport pandas as pdtruth = pd.Series(range(10))pred = pd.Series(range(10)) + np.random.random(10)# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputtruth = pd.Series(range(10))pred = pd.Series(range(10)) + np.random.random(10)# Solutionprint(np.mean((truth-pred)**2))
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.
Input:
PyInteractive Python
# Task: Convert the first character of each element in a series to uppercaseimport numpy as npimport pandas as pdser = pd.Series(['how', 'to', 'kick', 'ass?'])# Write your code below
Desired Output:
0 How
1 To
2 Kick
3 Ass?
dtype: object
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputser = pd.Series(['how', 'to', 'kick', 'ass?'])# Solution 1ser.map(lambda x: x.title())# Solution 2ser.map(lambda x: x[0].upper() + x[1:])# Solution 3print(pd.Series([i.title() for i in ser]))
19. How to calculate the number of characters in each word in a series?
Difficulty Level: L2
Input:
PyInteractive Python
# Task: Calculate the number of characters in each word in a seriesimport numpy as npimport pandas as pdser = pd.Series(['how', 'to', 'kick', 'ass?'])# Write your code below
Desired Output:
0 3
1 2
2 4
3 4
dtype: int64
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputser = pd.Series(['how', 'to', 'kick', 'ass?'])# Solutionprint(ser.map(lambda x: len(x)))
20. How to compute difference of differences between consequtive numbers of a series?
Difficulty Level: L1
Difference of differences between the consequtive numbers of ser.
Input:
PyInteractive Python
# Task: Compute difference of differences between consequtive numbers of a seriesimport numpy as npimport pandas as pdser = pd.Series([1, 3, 6, 10, 15, 21, 27, 35])# Write your code below
import numpy as npimport pandas as pd# Inputser = pd.Series([1, 3, 6, 10, 15, 21, 27, 35])# Solutionprint(ser.diff().tolist())print(ser.diff().diff().tolist())
21. How to convert a series of date-strings to a timeseries?
Difficiulty Level: L2
Input:
PyInteractive Python
# Task: Convert a series of date-strings to a timeseriesimport numpy as npimport pandas as pdser = pd.Series(['01 Jan 2010', '02-02-2011', '20120303', '2013/04/04', '2014-05-05', '2015-06-06T12:20'])# Write your code below
import numpy as np
import pandas as pd
# Input
ser = pd.Series(['01 Jan 2010', '02-02-2011', '20120303', '2013/04/04', '2014-05-05', '2015-06-06T12:20'])
# Solution 1from dateutil.parser import parse
ser.map(lambda x: parse(x))
# Solution 2print(pd.to_datetime(ser))
22. How to get the day of month, week number, day of year and day of week from a series of date strings?
Difficiulty Level: L2 Get the day of month, week number, day of year and day of week from ser.
Input:
PyInteractive Python
# Task: Get the day of month, week number, day of year and day of week from a series of date stringsimport numpy as npimport pandas as pdser = pd.Series(['01 Jan 2010', '02-02-2011', '20120303', '2013/04/04', '2014-05-05', '2015-06-06T12:20'])# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputser = pd.Series(['01 Jan 2010', '02-02-2011', '20120303', '2013/04/04', '2014-05-05', '2015-06-06T12:20'])# Solutionfrom dateutil.parser import parseser_ts = ser.map(lambda x: parse(x))# day of monthprint("Date: ", ser_ts.dt.day.tolist())# week numberprint("Week number: ", ser_ts.dt.isocalendar().week.tolist())# day of yearprint("Day num of year: ", ser_ts.dt.dayofyear.tolist())# day of weekprint("Day of week: ", ser_ts.dt.day_name().tolist())
23. How to convert year-month string to dates corresponding to the 4th day of the month?
Difficiulty Level: L2 Change ser to dates that start with 4th of the respective months.
Input:
PyInteractive Python
# Task: Convert year-month string to dates corresponding to the 4th day of the monthimport numpy as npimport pandas as pdser = pd.Series(['Jan 2010', 'Feb 2011', 'Mar 2012'])# Write your code below
import numpy as npimport pandas as pd# Inputser = pd.Series(['Jan 2010', 'Feb 2011', 'Mar 2012'])# Solution 1from dateutil.parser import parse# Parse the dateser_ts = ser.map(lambda x: parse(x))# Construct date string with date as 4ser_datestr = ser_ts.dt.year.astype('str') +'-'+ ser_ts.dt.month.astype('str') +'-'+'04'# Format it.[parse(i).strftime('%Y-%m-%d') for i in ser_datestr]# Solution 2print(ser.map(lambda x: parse('04 '+ x)))
24. How to filter words that contain atleast 2 vowels from a series?
Difficiulty Level: L3 From ser, extract words that contain atleast 2 vowels.
Input:
PyInteractive Python
# Task: Filter words that contain atleast 2 vowels from a seriesimport numpy as npimport pandas as pdser = pd.Series(['Apple', 'Orange', 'Plan', 'Python', 'Money'])# Write your code below
Desired Output:
0 Apple
1 Orange
4 Money
dtype: object
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputser = pd.Series(['Apple', 'Orange', 'Plan', 'Python', 'Money'])# Solutionfrom collections import Countermask = ser.map(lambda x: sum([Counter(x.lower()).get(i, 0) for i inlist('aeiou')]) >=2)print(ser[mask])
25. How to filter valid emails from a series?
Difficiulty Level: L3 Extract the valid emails from the series emails. The regex pattern for valid emails is provided as reference.
Input:
PyInteractive Python
# Task: Filter valid emails from a seriesimport numpy as npimport pandas as pdemails = pd.Series(['buying books at amazom.com', 'rameses@egypt.com', 'matt@t.co', 'narendra@modi.com'])pattern ='[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}'# Write your code below
import numpy as npimport pandas as pd# Inputemails = pd.Series(['buying books at amazom.com', 'rameses@egypt.com', 'matt@t.co', 'narendra@modi.com'])# Solution 1 (as series of strings)import repattern ='[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}'mask = emails.map(lambda x: bool(re.match(pattern, x)))emails[mask]# Solution 2 (as series of list)emails.str.findall(pattern, flags=re.IGNORECASE)# Solution 3 (as list)print([x[0] for x in [re.findall(pattern, email) for email in emails] iflen(x) >0])
26. How to get the mean of a series grouped by another series?
Difficiulty Level: L2 Compute the mean of weights of each fruit.
Input:
PyInteractive Python
# Task: Get the mean of a series grouped by another seriesimport numpy as npimport pandas as pdfruit = pd.Series(np.random.choice(['apple', 'banana', 'carrot'], 10))weights = pd.Series(np.linspace(1, 10, 10))print(weights.tolist())print(fruit.tolist())#> [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]#> ['banana', 'carrot', 'apple', 'carrot', 'carrot', 'apple', 'banana', 'carrot', 'apple', 'carrot']# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputfruit = pd.Series(np.random.choice(['apple', 'banana', 'carrot'], 10))weights = pd.Series(np.linspace(1, 10, 10))# Solutionprint(weights.groupby(fruit).mean())
27. How to compute the euclidean distance between two series?
Difficiulty Level: L2 Compute the euclidean distance between series (points) p and q, without using a packaged formula.
Input:
PyInteractive Python
# Task: Compute the euclidean distance between two seriesimport numpy as npimport pandas as pdp = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])q = pd.Series([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])# Write your code below
28. How to find all the local maxima (or peaks) in a numeric series?
Difficiulty Level: L3 Get the positions of peaks (values surrounded by smaller values on both sides) in ser.
Input:
PyInteractive Python
# Task: Find all the local maxima (or peaks) in a numeric seriesimport numpy as npimport pandas as pdser = pd.Series([2, 10, 3, 4, 9, 10, 2, 7, 3])# Write your code below
29. How to replace missing spaces in a string with the least frequent character?
Replace the spaces in my_str with the least frequent character. Difficiulty Level: L2
Input:
PyInteractive Python
# Task: Replace missing spaces in a string with the least frequent characterimport numpy as npimport pandas as pdmy_str ='dbc deb abed gade'# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputmy_str ='dbc deb abed gade'# Solutionser = pd.Series(list('dbc deb abed gade'))freq = ser.value_counts()print(freq)least_freq = freq.dropna().index[-1]print("".join(ser.replace(' ', least_freq)))
30. How to create a TimeSeries starting ‘2000-01-01’ and 10 weekends (saturdays) after that having random numbers as values?
Difficiulty Level: L2
Input:
PyInteractive Python
# Task: Create a TimeSeries starting '2000-01-01' and 10 weekends (saturdays) after that having random numbers as valuesimport numpy as npimport pandas as pd# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Solutionser = pd.Series(np.random.randint(1,10,10), pd.date_range('2000-01-01', periods=10, freq='W-SAT'))print(ser)
31. How to fill an intermittent time series so all missing dates show up with values of previous non-missing date?
Difficiulty Level: L2 ser has missing dates and values. Make all missing dates appear and fill up with value from previous date.
Input:
PyInteractive Python
# Task: Fill an intermittent time series so all missing dates show up with values of previous non-missing dateimport numpy as npimport pandas as pdser = pd.Series([1,10,3,np.nan], index=pd.to_datetime(['2000-01-01', '2000-01-03', '2000-01-06', '2000-01-08']))print(ser)#> 2000-01-01 1.0#> 2000-01-03 10.0#> 2000-01-06 3.0#> 2000-01-08 NaN#> dtype: float64# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputser = pd.Series([1,10,3, np.nan], index=pd.to_datetime(['2000-01-01', '2000-01-03', '2000-01-06', '2000-01-08']))# Solutionprint(ser.resample('D').ffill())
32. How to compute the autocorrelations of a numeric series?
Difficiulty Level: L3 Compute autocorrelations for the first 10 lags of ser. Find out which lag has the largest correlation.
Input:
PyInteractive Python
# Task: Compute the autocorrelations of a numeric seriesimport numpy as npimport pandas as pdser = pd.Series(np.arange(20) + np.random.normal(1, 10, 20))# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputser = pd.Series(np.arange(20) + np.random.normal(1, 10, 20))# Solutionautocorrelations = [ser.autocorr(i).round(2) for i inrange(11)]print(autocorrelations[1:])print('Lag having highest correlation: ', np.argmax(np.abs(autocorrelations[1:]))+1)
33. How to import only every nth row from a csv file to create a dataframe?
Difficiulty Level: L2 Import every 50th row of BostonHousing dataset as a dataframe.
Show Solution
Input:
import numpy as np
import pandas as pd
# Solution: Use chunks and list comprehension
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/BostonHousing.csv', chunksize=50)
df2 = pd.concat([chunk.iloc[0] for chunk in df], axis=1).T
print(df2.head())
34. How to change column values when importing csv to a dataframe?
Difficulty Level: L2
Import the boston housing dataset, but while importing change the ‘medv’ (median house value) column so that values 25 becomes ‘High’.
Show Solution
import numpy as np
import pandas as pd
# Solution 1: Using converter parameter
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/BostonHousing.csv',
converters={'medv': lambda x: 'High'iffloat(x) >25else'Low'})
# Solution 2: Using csv readerimport csv
withopen('BostonHousing.csv', 'r') as f:
reader = csv.reader(f)
out = []
for i, row inenumerate(reader):
if i >0:
row[13] ='High'iffloat(row[13]) >25else'Low'
out.append(row)
df = pd.DataFrame(out[1:], columns=out[0])
print(df.head())
35. How to create a dataframe with rows as strides from a given series?
Difficiulty Level: L3
PyInteractive Python
# Task: Create a dataframe with rows as strides from a given seriesimport numpy as npimport pandas as pdL = pd.Series(range(15))# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pdL = pd.Series(range(15))defgen_strides(a, stride_len=5, window_len=5): n_strides = ((a.size-window_len)//stride_len) +1return np.array([a[s:(s+window_len)] for s in np.arange(0, a.size, stride_len)[:n_strides]])print(gen_strides(L, stride_len=2, window_len=4))
36. How to import only specified columns from a csv file?
Difficulty Level: L1
Import ‘crim’ and ‘medv’ columns of the BostonHousing dataset as a dataframe.
Show Solution
Input:
import numpy as np
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/BostonHousing.csv', usecols=['crim', 'medv'])
print(df.head())
37. How to get the nrows, ncolumns, datatype, summary stats of each column of a dataframe? Also get the array and list equivalent.
Difficulty Level: L2
Get the number of rows, columns, datatype and summary statistics of each column of the Cars93 dataset. Also get the numpy array and list equivalent of the dataframe.
Show Solution
import numpy as np
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')
# number of rows and columnsprint(df.shape)
# datatypesprint(df.dtypes)
# how many columns under each dtypeprint(df.dtypes.value_counts())
# summary statistics
df_stats = df.describe()
# numpy array
df_arr = df.values
# list
df_list = df.values.tolist()
38. How to extract the row and column number of a particular cell with given criterion?
Difficulty Level: L1
Which manufacturer, model and type has the highest Price? What is the row and column number of the cell with the highest Price value?
# Task: Extract the row and column number of a particular cell with given criterionimport numpy as np
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')
# Write your code below
Show Solution
import numpy as np
import pandas as pd
# Input
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')
# Solution# Get Manufacturer with highest price
df.loc[df.Price == np.max(df.Price), ['Manufacturer', 'Model', 'Type']]
# Get Row and Column number
row, col = np.where(df.values == np.max(df.Price))
# Get the value
df.iat[row[0], col[0]]
df.iloc[row[0], col[0]]
# Alternatesprint(df.at[row[0], 'Price'])
# The difference between `iat` - `iloc` vs `at` - `loc` is:# `iat` snd `iloc` accepts row and column numbers.# Whereas `at` and `loc` accepts index and column names.
39. How to rename a specific columns in a dataframe?
Difficulty Level: L2
Rename the column Type as CarType in df and replace the ‘.’ in column names with ‘_’.
Input:
# Task: Rename a specific columns in a dataframeimport numpy as np
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')
print(df.columns)
#> Index(['Manufacturer', 'Model', 'Type', 'Min.Price', 'Price', 'Max.Price',#> 'MPG.city', 'MPG.highway', 'AirBags', 'DriveTrain', 'Cylinders',#> 'EngineSize', 'Horsepower', 'RPM', 'Rev.per.mile', 'Man.trans.avail',#> 'Fuel.tank.capacity', 'Passengers', 'Length', 'Wheelbase', 'Width',#> 'Turn.circle', 'Rear.seat.room', 'Luggage.room', 'Weight', 'Origin',#> 'Make'],#> dtype='object')# Write your code below
40. How to check if a dataframe has any missing values?
Difficulty Level: L1
Check if df has any missing values.
Input:
# Task: Check if a dataframe has any missing valuesimport numpy as np
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')
# Write your code below
Desired Output:
True
Show Solution
import numpy as np
import pandas as pd
# Input
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')
# Solutionprint(df.isnull().values.any())
41. How to count the number of missing values in each column?
Difficulty Level: L2
Count the number of missing values in each column of df. Which column has the maximum number of missing values?
Input:
# Task: Count the number of missing values in each columnimport numpy as np
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')
# Write your code below
42. How to replace missing values of multiple numeric columns with the mean?
Difficulty Level: L2
Replace missing values in Min.Price and Max.Price columns with their respective mean.
Input:
# Task: Replace missing values of multiple numeric columns with the meanimport numpy as np
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')
# Write your code below
43. How to use apply function on existing columns with global variables as additional arguments?
Difficulty Level: L3
In df, use apply method to replace the missing values in Min.Price with the column’s mean and those in Max.Price with the column’s median. Use Hint from StackOverflow
Input:
# Task: Use apply function on existing columns with global variables as additional argumentsimport numpy as np
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')
# Write your code below
44. How to select a specific column from a dataframe as a dataframe instead of a series?
Difficulty Level: L2
Get the first column (a) in df as a dataframe (rather than as a Series).
Input:
PyInteractive Python
# Task: Select a specific column from a dataframe as a dataframe instead of a seriesimport numpy as npimport pandas as pddf = pd.DataFrame(np.arange(20).reshape(-1, 5), columns=list('abcde'))# Write your code below
Desired Output:
pandas.core.series.Series
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputdf = pd.DataFrame(np.arange(20).reshape(-1, 5), columns=list('abcde'))# Solutiontype(df[['a']])type(df.loc[:, ['a']])type(df.iloc[:, [0]])# Alternately the following returns a Seriestype(df.a)type(df['a'])type(df.loc[:, 'a'])print(type(df.iloc[:, 1]))
45. How to change the order of columns of a dataframe?
Difficulty Level: L3
Actually 3 questions. Create a generic function to interchange two columns, without hardcoding column names. Sort the columns in reverse alphabetical order, that is colume ‘e’ first through column ‘a’ last.
Input:
PyInteractive Python
# Task: Change the order of columns of a dataframeimport numpy as npimport pandas as pddf = pd.DataFrame(np.arange(20).reshape(-1, 5), columns=list('abcde'))# Write your code below
46. How to set the number of rows and columns displayed in the output?
Difficulty Level: L2
Change the pamdas display settings on printing the dataframe df it shows a maximum of 10 rows and 10 columns.
Input:
# Task: Set the number of rows and columns displayed in the outputimport numpy as np
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')
# Write your code below
Show Solution
import numpy as np
import pandas as pd
# Input
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')
# Solution
pd.set_option('display.max_columns', 10)
pd.set_option('display.max_rows', 10)
print(df)
# df# Show all available options# pd.describe_option()
47. How to format or suppress scientific notations in a pandas dataframe?
Difficulty Level: L2
Suppress scientific notations like ‘e-03’ in df and print upto 4 numbers after decimal.
Input:
PyInteractive Python
# Task: Format or suppress scientific notations in a pandas dataframeimport numpy as npimport pandas as pddf = pd.DataFrame(np.random.random(4)**10, columns=['random'])df#> random#> 0 3.474280e-03#> 1 3.951517e-05#> 2 7.469702e-02#> 3 5.541282e-28# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputdf = pd.DataFrame(np.random.random(4)**10, columns=['random'])# Solution 1: Roundingdf.round(4)# Solution 2: Use apply to change formatdf.apply(lambda x: '%.4f'% x, axis=1)# ordf.applymap(lambda x: '%.4f'% x)# Solution 3: Use set_optionpd.set_option('display.float_format', lambda x: '%.4f'% x)# Solution 4: Assign display.float_formatpd.options.display.float_format ='{:.4f}'.formatprint(df)# Reset/undo float formattingpd.options.display.float_format =None
48. How to format all the values in a dataframe as percentages?
Difficulty Level: L2
Format the values in column ‘random’ of df as percentages.
Input:
PyInteractive Python
# Task: Format all the values in a dataframe as percentagesimport numpy as npimport pandas as pddf = pd.DataFrame(np.random.random(4), columns=['random'])df#> random#> 0 .689723#> 1 .957224#> 2 .159157#> 3 .21082# Write your code below
Show Solution
import numpy as np
import pandas as pd
# Input
df = pd.DataFrame(np.random.random(4), columns=['random'])
# Solution
out = df.style.format({
'random': '{0:.2%}'.format,
})
print(out)
49. How to filter every nth row in a dataframe?
Difficulty Level: L1
From df, filter the ‘Manufacturer’, ‘Model’ and ‘Type’ for every 20th row starting from 1st (row 0).
Input:
# Task: Filter every nth row in a dataframeimport numpy as np
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')
# Write your code below
Desired Output:
Manufacturer Model Type
0 Acura Integra Small
20 Chrysler LeBaron Compact
40 Honda Prelude Sporty
60 Mercury Cougar Midsize
80 Subaru Loyale Small
Show Solution
import numpy as np
import pandas as pd
# Input
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')
# Solutionprint(df.iloc[::20, :][['Manufacturer', 'Model', 'Type']])
50. How to create a primary key index by combining relevant columns?
Difficulty Level: L2
In df, Replace NaNs with ‘missing’ in columns ‘Manufacturer’, ‘Model’ and ‘Type’ and create a index as a combination of these three columns and check if the index is a primary key.
Input:
# Task: Create a primary key index by combining relevant columnsimport numpy as np
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv', usecols=[0,1,2,3,5])
# Write your code below
51. How to get the row number of the nth largest value in a column?
Difficulty Level: L2
Find the row position of the 5th largest value of column ‘a’ in df.
Input:
PyInteractive Python
# Task: Get the row number of the nth largest value in a columnimport numpy as npimport pandas as pddf = pd.DataFrame(np.random.randint(1, 30, 30).reshape(10,-1), columns=list('abc'))# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputdf = pd.DataFrame(np.random.randint(1, 30, 30).reshape(10,-1), columns=list('abc'))# Solutionn =5print(df['a'].argsort()[::-1][n])
52. How to find the position of the nth largest value greater than a given value?
Difficulty Level: L2
In ser, find the position of the 2nd largest value greater than the mean.
Input:
PyInteractive Python
# Task: Find the position of the nth largest value greater than a given valueimport numpy as npimport pandas as pdser = pd.Series(np.random.randint(1, 100, 15))# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputser = pd.Series(np.random.randint(1, 100, 15))# Solutionprint('ser: ', ser.tolist(), 'mean: ', round(ser.mean()))print(np.argwhere(ser > ser.mean())[1])
53. How to get the last n rows of a dataframe with row sum > 100?
Difficulty Level: L2
Get the last two rows of df whose row sum is greater than 100.
Input:
PyInteractive Python
# Task: Get the last n rows of a dataframe with row sum > 100import numpy as npimport pandas as pddf = pd.DataFrame(np.random.randint(10, 40, 60).reshape(-1, 4))# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputdf = pd.DataFrame(np.random.randint(10, 40, 60).reshape(-1, 4))# Solution# print row sumsrowsums = df.apply(np.sum, axis=1)# last two rows with row sum greater than 100last_two_rows = df.iloc[np.where(rowsums >100)[0][-2:], :]
54. How to find and cap outliers from a series or dataframe column?
Difficulty Level: L2
Replace all values of ser in the lower 5%ile and greater than 95%ile with respective 5th and 95th %ile value.
Input:
PyInteractive Python
# Task: Find and cap outliers from a series or dataframe columnimport numpy as npimport pandas as pdser = pd.Series(np.logspace(-2, 2, 30))# Write your code below
55. How to reshape a dataframe to the largest possible square after removing the negative values?
Difficulty Level: L3
Reshape df to the largest possible square with negative values removed. Drop the smallest values if need be. The order of the positive numbers in the result should remain the same as the original.
Input:
PyInteractive Python
# Task: Reshape a dataframe to the largest possible square after removing the negative valuesimport numpy as npimport pandas as pddf = pd.DataFrame(np.random.randint(-20, 50, 100).reshape(10,-1))# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputdf = pd.DataFrame(np.random.randint(-20, 50, 100).reshape(10,-1))print(df)# Solution# Step 1: remove negative values from arrarr = df[df >0].values.flatten()arr_qualified = arr[~np.isnan(arr)]# Step 2: find side-length of largest possible squaren =int(np.floor(arr_qualified.shape[0]**.5))# Step 3: Take top n^2 items without changing positionstop_indexes = np.argsort(arr_qualified)[::-1]output = np.take(arr_qualified, sorted(top_indexes[:n**2])).reshape(n, -1)print(output)
56. How to swap two rows of a dataframe?
Difficulty Level: L2
Swap rows 1 and 2 in df.
Input:
PyInteractive Python
# Task: Swap two rows of a dataframeimport numpy as npimport pandas as pddf = pd.DataFrame(np.arange(25).reshape(5, -1))# Write your code below
import numpy as npimport pandas as pd# Inputdf = pd.DataFrame(np.arange(25).reshape(5, -1))# Solution 1df.iloc[::-1, :]# Solution 2print(df.loc[df.index[::-1], :])
58. How to create one-hot encodings of a categorical variable (dummy variables)?
Difficulty Level: L2
Get one-hot encodings for column ‘a’ in the dataframe df and append it as columns. Output
Input:
PyInteractive Python
# Task: Create one-hot encodings of a categorical variable (dummy variables)import numpy as npimport pandas as pddf = pd.DataFrame(np.arange(25).reshape(5,-1), columns=list('abcde'))# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputdf = pd.DataFrame(np.arange(25).reshape(5,-1), columns=list('abcde'))# Solutiondf_onehot = pd.concat([pd.get_dummies(df['a']), df[list('bcde')]], axis=1)print(df_onehot)
59. Which column contains the highest number of row-wise maximum values?
Difficulty Level: L2
Obtain the column name with the highest number of row-wise maximum’s in df.
Input:
PyInteractive Python
# Task: Which column contains the highest number of row-wise maximum valuesimport numpy as npimport pandas as pddf = pd.DataFrame(np.random.randint(1,100, 40).reshape(10, -1))# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputdf = pd.DataFrame(np.random.randint(1,100, 40).reshape(10, -1))# Solutionprint('Column with highest row maxes: ', df.idxmax(axis=1).value_counts().index[0])
60. How to create a new column that contains the row number of nearest column by euclidean distance?
Difficulty Level: L3
Create a new column such that, each row contains the row number of nearest row-record by euclidean distance.
Input:
PyInteractive Python
# Task: Create a new column that contains the row number of nearest column by euclidean distanceimport numpy as npimport pandas as pddf = pd.DataFrame(np.random.randint(1,100, 40).reshape(10, -1), columns=list('pqrs'), index=list('abcdefghij'))df# p q r s# a 57 77 13 62# b 68 5 92 24# c 74 40 18 37# d 80 17 39 60# e 93 48 85 33# f 69 55 8 11# g 39 23 88 53# h 63 28 25 61# i 18 4 73 7# j 79 12 45 34# Write your code below
Show Solution
import pandas as pd
df = pd.DataFrame(np.random.randint(1,100, 40).reshape(10, -1), columns=list('pqrs'), index=list('abcdefghij'))
# Solutionimport numpy as np
# init outputs
nearest_rows = []
nearest_distance = []
# iterate rows.for i, row in df.iterrows():
curr = row
rest = df.drop(i)
e_dists = {} # init dict to store euclidean dists for current row.# iterate rest of rows for current rowfor j, contestant in rest.iterrows():
# compute euclidean dist and update e_dists
e_dists.update({j: round(np.linalg.norm(curr.values - contestant.values))})
# update nearest row to current row and the distance value
nearest_rows.append(min(e_dists, key=e_dists.get))
nearest_distance.append(min(e_dists.values()))
df['nearest_row'] = nearest_rows
df['dist'] = nearest_distance
print(df)
61. How to know the maximum possible correlation value of each column against other columns?
Difficulty Level: L2
Compute maximum possible absolute correlation value of each column against other columns in df.
Input:
PyInteractive Python
# Task: Know the maximum possible correlation value of each column against other columnsimport numpy as npimport pandas as pddf = pd.DataFrame(np.random.randint(1,100, 80).reshape(8, -1), columns=list('pqrstuvwxy'), index=list('abcdefgh'))# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputdf = pd.DataFrame(np.random.randint(1,100, 80).reshape(8, -1), columns=list('pqrstuvwxy'), index=list('abcdefgh'))df# Solutionabs_corrmat = np.abs(df.corr())max_corr = abs_corrmat.apply(lambda x: sorted(x)[-2])print('Maximum Correlation possible for each column: ', np.round(max_corr.tolist(), 2))
62. How to create a column containing the minimum by maximum of each row?
Difficulty Level: L2
Compute the minimum-by-maximum for every row of df.
Input:
PyInteractive Python
# Task: Create a column containing the minimum by maximum of each rowimport numpy as npimport pandas as pddf = pd.DataFrame(np.random.randint(1,100, 80).reshape(8, -1))# Write your code below
63. How to create a column that contains the penultimate value in each row?
Difficulty Level: L2
Create a new column ‘penultimate’ which has the second largest value of each row of df.
Input:
PyInteractive Python
# Task: Create a column that contains the penultimate value in each rowimport numpy as npimport pandas as pddf = pd.DataFrame(np.random.randint(1,100, 80).reshape(8, -1))# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputdf = pd.DataFrame(np.random.randint(1,100, 80).reshape(8, -1))# Solutionout = df.apply(lambda x: x.sort_values().unique()[-2], axis=1)df['penultimate'] = outprint(df)
64. How to normalize all columns in a dataframe?
Difficulty Level: L2
Don’t use external packages like sklearn.
Input:
PyInteractive Python
# Task: Normalize all columns in a dataframeimport numpy as npimport pandas as pddf = pd.DataFrame(np.random.randint(1,100, 80).reshape(8, -1))# Write your code below
65. How to compute the correlation of each row with the suceeding row?
Difficulty Level: L2
Compute the correlation of each row of df with its succeeding row.
Input:
PyInteractive Python
# Task: Compute the correlation of each row with the suceeding rowimport numpy as npimport pandas as pddf = pd.DataFrame(np.random.randint(1,100, 80).reshape(8, -1))# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputdf = pd.DataFrame(np.random.randint(1,100, 80).reshape(8, -1))# Solutionprint([df.iloc[i].corr(df.iloc[i+1]).round(2) for i inrange(df.shape[0])[:-1]])
66. How to replace both the diagonals of dataframe with 0?
Difficulty Level: L2
Replace both values in both diagonals of df with 0.
import numpy as npimport pandas as pd# Inputdf = pd.DataFrame(np.random.randint(1,100, 100).reshape(10, -1))# Solutionfor i inrange(df.shape[0]): df.iat[i, i] =0 df.iat[df.shape[0]-i-1, i] =0print(df)
67. How to get the particular group of a groupby dataframe by key?
Difficulty Level: L2
This is a question related to understanding of grouped dataframe. From df_grouped, get the group belonging to ‘apple’ as a dataframe.
Input:
PyInteractive Python
# Task: Get the particular group of a groupby dataframe by keyimport numpy as npimport pandas as pddf = pd.DataFrame({'col1': ['apple', 'banana', 'orange'] *3,'col2': np.random.rand(9),'col3': np.random.randint(0, 15, 9)})df_grouped = df.groupby(['col1'])# Write your code below
Show Solution
import numpy as np
import pandas as pd
# Input
df = pd.DataFrame({'col1': ['apple', 'banana', 'orange'] *3,
'col2': np.random.rand(9),
'col3': np.random.randint(0, 15, 9)})
df_grouped = df.groupby(['col1'])
# Solution 1
df_grouped.get_group('apple')
# Solution 2for i, dff in df_grouped:
if i =='apple':
print(dff)
68. How to get the n’th largest value of a column when grouped by another column?
Difficulty Level: L2
In df, find the second largest value of ‘taste’ for ‘banana’
Input:
PyInteractive Python
# Task: Get the n'th largest value of a column when grouped by another columnimport numpy as npimport pandas as pddf = pd.DataFrame({'fruit': ['apple', 'banana', 'orange'] *3,'taste': np.random.rand(9),'price': np.random.randint(0, 15, 9)})# Write your code below
69. How to compute grouped mean on pandas dataframe and keep the grouped column as another column (not index)?
Difficulty Level: L1
In df, Compute the mean price of every fruit, while keeping the fruit as another column instead of an index.
Input:
PyInteractive Python
# Task: Compute grouped mean on pandas dataframe and keep the grouped column as another column (not index)import numpy as npimport pandas as pddf = pd.DataFrame({'fruit': ['apple', 'banana', 'orange'] *3,'rating': np.random.rand(9),'price': np.random.randint(0, 15, 9)})# Write your code below
70. How to join two dataframes by 2 columns so they have only the common rows?
Difficulty Level: L2
Join dataframes df1 and df2 by ‘fruit-pazham’ and ‘weight-kilo’.
Input:
PyInteractive Python
# Task: Join two dataframes by 2 columns so they have only the common rowsimport numpy as npimport pandas as pddf1 = pd.DataFrame({'fruit': ['apple', 'banana', 'orange'] *3,'weight': ['high', 'medium', 'low'] *3,'price': np.random.randint(0, 15, 9)})df2 = pd.DataFrame({'pazham': ['apple', 'orange', 'pine'] *2,'kilo': ['high', 'low'] *3,'price': np.random.randint(0, 15, 6)})# Write your code below
73. How to create lags and leads of a column in a dataframe?
Difficulty Level: L2
Create two new columns in df, one of which is a lag1 (shift column a down by 1 row) of column ‘a’ and the other is a lead1 (shift column b up by 1 row).
PyInteractive Python
# Task: Create lags and leads of a column in a dataframeimport numpy as npimport pandas as pddf = pd.DataFrame(np.random.randint(1, 100, 20).reshape(-1, 4), columns =list('abcd'))# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputdf = pd.DataFrame(np.random.randint(1, 100, 20).reshape(-1, 4), columns =list('abcd'))# Solutiondf['a_lag1'] = df['a'].shift(1)df['b_lead1'] = df['b'].shift(-1)print(df)
74. How to get the frequency of unique values in the entire dataframe?
Difficulty Level: L2
Get the frequency of unique values in the entire dataframe df.
Input:
PyInteractive Python
# Task: Get the frequency of unique values in the entire dataframeimport numpy as npimport pandas as pddf = pd.DataFrame(np.random.randint(1, 10, 20).reshape(-1, 4), columns =list('abcd'))# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputdf = pd.DataFrame(np.random.randint(1, 10, 20).reshape(-1, 4), columns =list('abcd'))# Solutionprint(pd.value_counts(df.values.ravel()))
75. How to split a text column into two separate columns?
Difficulty Level: L2
Split the string column in df to form a dataframe with 3 columns as shown.
Input:
PyInteractive Python
# Task: Split a text column into two separate columnsimport numpy as npimport pandas as pddf = pd.DataFrame(["STD, City State","33, Kolkata West Bengal","44, Chennai Tamil Nadu","40, Hyderabad Telengana","80, Bangalore Karnataka"], columns=['row'])print(df)#> row#> 0 STD, City\tState#> 1 33, Kolkata\tWest Bengal#> 2 44, Chennai\tTamil Nadu#> 3 40, Hyderabad\tTelengana#> 4 80, Bangalore\tKarnataka# Write your code below
Show Solution
PyInteractive Python
import numpy as npimport pandas as pd# Inputdf = pd.DataFrame(["STD, City State","33, Kolkata West Bengal","44, Chennai Tamil Nadu","40, Hyderabad Telengana","80, Bangalore Karnataka"], columns=['row'])# Solutiondf_out = df.row.str.split(',\s+', expand=True)# Make first row as headernew_header = df_out.iloc[0]df_out = df_out[1:]df_out.columns = new_headerprint(df_out)