[go: up one dir, main page]

0% found this document useful (0 votes)
43 views13 pages

Pandas Series Exercises & Solutions

The document provides a series of exercises and solutions for using the Pandas library in Python, focusing on creating and manipulating data series. Each exercise includes sample code demonstrating various operations such as creating series, converting between types, performing arithmetic, and sorting. The exercises aim to enhance understanding of data handling in Pandas for practical data analysis.

Uploaded by

Sydney Ochieng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views13 pages

Pandas Series Exercises & Solutions

The document provides a series of exercises and solutions for using the Pandas library in Python, focusing on creating and manipulating data series. Each exercise includes sample code demonstrating various operations such as creating series, converting between types, performing arithmetic, and sorting. The exercises aim to enhance understanding of data handling in Pandas for practical data analysis.

Uploaded by

Sydney Ochieng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Pandas Exercises, Practice, Solution

pandas is a Python package providing fast, flexible, and expressive data


structures designed to make working with 'relationa' or 'labeled' data both
easy and intuitive. It aims to be the fundamental high-level building block for
doing practical, real world data analysis in Python.

Pandas: Data Series Exercise-1 with Solution


Write a Pandas program to create and display a one-dimensional array-like object
containing an array of data.

Sample Solution :

Python Code :
import pandas as pd

ds = pd.Series([2, 4, 6, 8, 10])

print(ds)
Pandas: Data Series Exercise-2 with Solution
Write a Pandas program to convert a Panda module Series to Python list and
it’s type.

Sample Solution :

Python Code :
import pandas as pd

ds = pd.Series([2, 4, 6, 8, 10])

print("Pandas Series and type")

print(ds)

print(type(ds))

print("Convert Pandas Series to Python list")

print(ds.tolist())

print(type(ds.tolist()))
Pandas: Data Series Exercise-3 with Solution
Write a Pandas program to add, subtract, multiple and divide two Pandas
Series.

Sample Series: [2, 4, 6, 8, 10], [1, 3, 5, 7, 9]

Sample Solution :

Python Code :
import pandas as pd

ds1 = pd.Series([2, 4, 6, 8, 10])

ds2 = pd.Series([1, 3, 5, 7, 9])

ds = ds1 + ds2

print("Add two Series:")

print(ds)

print("Subtract two Series:")

ds = ds1 - ds2

print(ds)

print("Multiply two Series:")

ds = ds1 * ds2

print(ds)

print("Divide Series1 by Series2:")

ds = ds1 / ds2

print(ds)
Pandas: Data Series Exercise-4 with Solution
Write a Pandas program to compare the elements of the two Pandas Series.

Sample Series: [2, 4, 6, 8, 10], [1, 3, 5, 7, 10]

Sample Solution:

Python Code :
import pandas as pd

ds1 = pd.Series([2, 4, 6, 8, 10])

ds2 = pd.Series([1, 3, 5, 7, 10])

print("Series1:")

print(ds1)

print("Series2:")

print(ds2)

print("Compare the elements of the said Series:")

print("Equals:")

print(ds1 == ds2)

print("Greater than:")

print(ds1 > ds2)

print("Less than:")

print(ds1 < ds2)


Pandas: Data Series Exercise-5 with Solution
Write a Pandas program to convert a dictionary to a Pandas series.

Sample dictionary: d1 = {'a': 100, 'b': 200, 'c':300, 'd':400, 'e':800}

Sample Solution :

Python Code :
import pandas as pd

d1 = {'a': 100, 'b': 200, 'c':300, 'd':400, 'e':800}

print("Original dictionary:")

print(d1)

new_series = pd.Series(d1)

print("Converted series:")

print(new_series)
Pandas: Data Series Exercise-6 with Solution
Write a Pandas program to convert a NumPy array to a Pandas series.

Sample NumPy array: d1 = [10, 20, 30, 40, 50]


Sample Solution :

Python Code :
import numpy as np

import pandas as pd

np_array = np.array([10, 20, 30, 40, 50])

print("NumPy array:")

print(np_array)

new_series = pd.Series(np_array)

print("Converted Pandas series:")

print(new_series)
Pandas: Data Series Exercise-7 with Solution
Write a Pandas program to change the data type of given a column or a
Series.

Sample Series:
Original Data Series:
0 100
1 200
2 python
3 300.12
4 400
dtype: object
Change the said data type to numeric:
0 100.00
1 200.00
2 NaN
3 300.12
4 400.00
dtype: float64

Sample Solution :

Python Code :
import pandas as pd

s1 = pd.Series(['100', '200', 'python', '300.12', '400'])

print("Original Data Series:")

print(s1)

print("Change the said data type to numeric:")

s2 = pd.to_numeric(s1, errors='coerce')

print(s2)
Pandas: Data Series Exercise-8 with Solution
Write a Pandas program to convert the first column of a DataFrame as a
Series.

Sample Solution :

Python Code :
import pandas as pd

d = {'col1': [1, 2, 3, 4, 7, 11], 'col2': [4, 5, 6, 9, 5, 0],


'col3': [7, 5, 8, 12, 1,11]}

df = pd.DataFrame(data=d)

print("Original DataFrame")

print(df)

s1 = df.ix[:,0]

print("\n1st column as a Series:")

print(s1)

print(type(s1))
Pandas: Data Series Exercise-9 with Solution
Write a Pandas program to convert a given Series to an array.

Sample Solution :

Python Code :
import pandas as pd

import numpy as np

s1 = pd.Series(['100', '200', 'python', '300.12', '400'])

print("Original Data Series:")

print(s1)

print("Series to an array")

a = np.array(s1.values.tolist())

print (a)
Pandas: Data Series Exercise-10 with Solution
Write a Pandas program to convert Series of lists to one Series.

Sample Solution :

Python Code :
import pandas as pd

s = pd.Series([

['Red', 'Green', 'White'],

['Red', 'Black'],

['Yellow']])

print("Original Series of list")

print(s)

s = s.apply(pd.Series).stack().reset_index(drop=True)

print("One Series")

print(s)
Pandas: Data Series Exercise-11 with Solution
Write a Pandas program to sort a given Series.

Sample Solution :

Python Code :
import pandas as pd

s = pd.Series(['100', '200', 'python', '300.12', '400'])

print("Original Data Series:")

print(s)

new_s = pd.Series(s).sort_values()

print(new_s)
Pandas: Data Series Exercise-12 with Solution
Write a Pandas program to add some data to an existing Series.

Sample Solution :

Python Code :
import pandas as pd

s = pd.Series(['100', '200', 'python', '300.12', '400'])

print("Original Data Series:")

print(s)

print("\nData Series after adding some data:")

new_s = s.append(pd.Series(['500', 'php']))

print(new_s)

You might also like