0% found this document useful (0 votes)
3 views12 pages

IP Practical Programs

Uploaded by

mistdark06
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)
3 views12 pages

IP Practical Programs

Uploaded by

mistdark06
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

PYTHON PRACTICAL PROGRAMS

DATA HANDLING USING PANDAS PROGRAMS

1. Create a pandas series from a dictionary of values and a ndarray.

import pandas as pd
import numpy as np

#Creating series from a dictionary


d={'Jan':31,'Feb':28,'Mar':31,'Apr':30}
s=[Link](d)
print("Series from dictionary")
print("~~~~~~~~~~~~~~~~~~~~~~~")
print(s)

#Creating series from an ndarray


ar=[Link]([2,3,4,5,6])
print("\nSeries from ndarray")
print("~~~~~~~~~~~~~~~~~~~~~~~")
s1=[Link](ar)
print(s1)
Output:

Create PDF files without this message by purchasing novaPDF printer ([Link]
2. Write a Pandas program to multiply and divide two Pandas Series.

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

import pandas as pd
ds1 = [Link]([2, 4, 8, 10])
ds2 = [Link]([1, 3, 7, 9])

print("Multiply two Series:")


ds = ds1 * ds2
print(ds)

print("Divide Series1 by Series2:")


ds = ds1 / ds2
print(ds)

Output:

Create PDF files without this message by purchasing novaPDF printer ([Link]
3. Write a Pandas program to sort a given Series.
400, 300.12,100, 200

import pandas as pd
s = [Link]([400, 300.12,100, 200])
print("Original Data Series:")
print(s)
new_s = [Link](s).sort_values()
print(new_s)

Output:

[Link] a Pandas program to change the order of index of a given series.

Original Data Series:

A 1
B 2
C 3
dtype: int64
Data Series after changing the order of index:

B 2
A 1
C 3
dtype: int64

import pandas as pd

s = [Link](data = [1,2,3], index = ['A', 'B', 'C'])


print("Original Data Series:")
print(s)

Create PDF files without this message by purchasing novaPDF printer ([Link]
s = [Link](index = ['B','A','C'])
print("Data Series after changing the order of index:")
print(s)
Output:

5. Write a Pandas program to get the first 3 rows of a given DataFrame.

import pandas as pd

import numpy as np

exam_data = {'name': ['Manish', 'Dhiraj','Man', 'Dhir'],

'score': [12.5, 91,2.5, 9]}

df = [Link](exam_data )

print("First three rows of the data frame:")

print([Link][:3])

#print([Link](3))

Output:

6. Write a Pandas program to count the number of rows and columns of a DataFrame.

import pandas as pd
import numpy as np
exam_data = {'name': ['Manish', 'Dhiraj','Man', 'Dhir'],
'score': [12.5, 91,2.5, 9]}

Create PDF files without this message by purchasing novaPDF printer ([Link]
df = [Link](exam_data )
total_rows=len([Link][0])
total_cols=len([Link][1])
print("Number of Rows: "+str(total_rows))
print("Number of Columns: "+str(total_cols))

Output:

7. Write a Pandas program to select the rows the score is between 15 and 20 (inclusive)

import pandas as pd

import numpy as np

exam_data = {'name': ['Manish', 'Dhiraj','Man', 'Dhir'],

'score': [12.5, 91,20.5, 19]}

df = [Link](exam_data )

print("Rows where score between 15 and 20 (inclusive):")

print(df[df['score'].between(15, 20)])

Output:

8. Write a Pandas program to change the name 'Manish' to 'Anish' in name column and to
insert a new column in the existing data frame.

import pandas as pd
import numpy as np
exam_data = {'name': ['Manish', 'Dhiraj','Man', 'Dhir'],
'score': [12.5, 91,20.5, 19]}
df = [Link](exam_data )

Create PDF files without this message by purchasing novaPDF printer ([Link]
print(df,'\n')
df['name'] = df['name'].replace('Manish', 'Anish')
print("Data in dataframe after editing:\n",df)
medium = ['english','hindi','hindi','english']
df['medium'] = medium
print("\nNewDataFrame after inserting the 'medium' column")
print(df)

Output:

9. Write a Pandas program to showDataFrame row(s) based on given column


value/condition.

import pandas as pd
import numpy as np
exam_data = {'name': ['Manish', 'Dhiraj','Man', 'Dhir'],
'score': [12.5, 91,20.5, 19]}
df = [Link](exam_data )
print(df)
# Applying condition
df1 = df[[Link]>= 20]
print("New DataFrame:")
print(df1)

Create PDF files without this message by purchasing novaPDF printer ([Link]
Output:

10. Given a Series, print all the elements that are above the 75th percentile.

import pandas as pd

std_marks = []
for i in range(1,6):
m = int(input("Enter the Percentile:"))
std_marks.append(m)
s = [Link](index=range(1,6),data=std_marks)
print("Data fetched from the series are:")
print(s[s>=75])

Output:

Create PDF files without this message by purchasing novaPDF printer ([Link]
11. Write a Pandas program to combining two series into a DataFrame.
import pandas as pd
import numpy as np
s1 = [Link](['100', '200', '400'])
s2 = [Link](['10', '20', '40'])
print("Data Series:")
print(s1)
print(s2)
df = [Link]([s1, s2], axis=1)
print("New DataFrame combining two series:")
print(df)
Output:

Create PDF files without this message by purchasing novaPDF printer ([Link]
12. Create a data frame for examination results and display row labels, column labels data
types of each column and the dimensions.

Output:

Create PDF files without this message by purchasing novaPDF printer ([Link]
13. Create the following DataFrameExam_Result containing subject wise marks for each
student. Use the subjects as column labels, and Students roll numbers as row labels.

English Physics Chemistry Optional

1001 89 78 68 80

1002 68 90 78 87

1003 58 70 69 59

1004 66 55 77 78

1) Create the DataFrame.


2) Display the row labels of Sales.
3) Display the column labels of Sales.
4) Display the data types of each column of Sales.
5) Display the dimensions, shape, size and values of Sales.

Create PDF files without this message by purchasing novaPDF printer ([Link]
Output:

14. Write a program to filter out rows on different criteria such as duplicate rows from a
DataFrame.

Create PDF files without this message by purchasing novaPDF printer ([Link]
Output:

15. Write a python pandas program to create data frame quarterly sales where each row
contain the item category, item name and expenditure. Group the row by the category and
print the total expenditure per category.

Output:

Create PDF files without this message by purchasing novaPDF printer ([Link]

You might also like