0% found this document useful (0 votes)
4 views4 pages

pandas notes

The document provides a step-by-step guide on manipulating a Pandas Series, including creating a Series from a dictionary, updating values, adding new entries, and deleting existing ones. It also covers slicing, performing mathematical operations, checking for null values, reindexing, and renaming the Series. The output demonstrates the results of each operation performed on the Series.

Uploaded by

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

pandas notes

The document provides a step-by-step guide on manipulating a Pandas Series, including creating a Series from a dictionary, updating values, adding new entries, and deleting existing ones. It also covers slicing, performing mathematical operations, checking for null values, reindexing, and renaming the Series. The output demonstrates the results of each operation performed on the Series.

Uploaded by

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

import pandas as pd

# a) Create the Series from a dictionary


marks = pd.Series({'Math': 90, 'Science': 80, 'English': 85})
print("a) Original Series:")
print(marks)

# b) Change the marks of 'Science' to 88


marks['Science'] = 88
print("\nb) After updating Science marks to 88:")
print(marks)

# c) Add a new subject 'History' with marks 75


marks['History'] = 75
print("\nc) After adding History with marks 75:")
print(marks)

# d) Delete the 'English' entry


marks = marks.drop('English')
print("\nd) After deleting the English entry:")
print(marks)
Output
a) Original Series:
Math 90
Science 80
English 85
dtype: int64

b) After updating Science marks to 88:


Math 90
Science 88
English 85
dtype: int64

c) After adding History with marks 75:


Math 90
Science 88
English 85
History 75
dtype: int64

d) After deleting the English entry:


Math 90
Science 88
History 75
dtype: int64

Questions with solution


import pandas as pd
import numpy as np
# Define a Series with some integers
data = pd.Series([10, 20, 30, 40, 50], index=['a', 'b', 'c', 'd',
'e'])
print(data)

a) Slicing using labels and integers


python
# Slicing using labels (inclusive)
print("\nSlicing using labels (from 'b' to 'd'):")
print(data['b':'d'])

# Slicing using integer positions (exclusive of upper bound)


print("\nSlicing using integer positions (from 1 to 4):")
print(data[1:4])

b) Mathematical operations with a scalar


# Adding 5 to each element
print("\nAdding 5 to each element:")
print(data + 5)

# Multiplying each element by 2


print("\nMultiplying each element by 2:")
print(data * 2)

c) Checking for null values


# Introducing a null value
data1 = pd.Series([10, 20, np.nan, 40, 50], index=['a', 'b', 'c',
'd', 'e'])
print("\nSeries with a NaN value:")
print(data1)

# Checking for null values


print("\nChecking for null values using isnull():")
print(data1.isnull())

d) Reindexing the Series


# Reindexing with a new index list (adds NaN for missing indices)
new_index = ['a', 'b', 'c', 'd', 'e', 'f']
reindexed_data = data.reindex(new_index)
print("\nReindexed Series:")
print(reindexed_data)

e) Renaming the Series


# Renaming the Series
data.name = "Marks_Series"
print("\nRenamed Series:")
print(data)

outputs:

a 10
b 20
c 30
d 40
e 50
dtype: int64

Slicing using labels (from 'b' to 'd'):


b 20
c 30
d 40
dtype: int64

Slicing using integer positions (from 1 to 4):


b 20
c 30
d 40
dtype: int64

Adding 5 to each element:


a 15
b 25
c 35
d 45
e 55
dtype: int64

Multiplying each element by 2:


a 20
b 40
c 60
d 80
e 100
dtype: int64

Series with a NaN value:


a 10.0
b 20.0
c NaN
d 40.0
e 50.0
dtype: float64

Checking for null values using isnull():


a False
b False
c True
d False
e False
dtype: bool

Reindexed Series:
a 10.0
b 20.0
c 30.0
d 40.0
e 50.0
f NaN
dtype: float64

Renamed Series:
a 10
b 20
c 30
d 40
e 50
Name: Marks_Series, dtype: int64

You might also like