[go: up one dir, main page]

0% found this document useful (0 votes)
16 views11 pages

Practicals

Uploaded by

ARSH Chawaria
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)
16 views11 pages

Practicals

Uploaded by

ARSH Chawaria
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/ 11

# Create a panda’s series from a dictionary of values and a ndarray

import pandas as pd
import numpy as np
s=pd.Series(np.array([1,3,4,7,8,8,9]))
print(s)

# create a dictionary
dictionary = {'X' : 10, 'Y' : 20, 'Z' : 30} # create a series
series = pd.Series(dictionary)
print(series)

OUTPUT:

0 1

1 3

2 4

3 7

4 8

5 8

6 9

dtype: int64

X 10

Y 20

Z 30

dtype: int64
# Write a Pandas program to perform arithmetic operations on two Pandas
Series.
import pandas as pd
ds1 = pd.Series([3, 6, 9, 12, 15])
ds2 = pd.Series([2, 4, 6, 8, 10])
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)

OUTPUT:

Add two Series: Multiply two Series:

0 5 0 6

1 10 1 24

2 15 2 54

3 20 3 96

4 25 4 150

dtype: int64 dtype: int64

Subtract two Series:

0 1

1 2

2 3

3 4

4 5

Dtype:64
# Write a Pandas program to select the rows where the percentage
greater than 70.
import pandas as pd
import numpy as np

exam_data = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit',


'Sumit', 'Matthew', 'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes',
'no', 'no', 'yes']}
labels = ['A', 'B', 'C', 'B', 'E', 'F', 'G', 'H', 'I', 'J']

df = pd.DataFrame(exam_data , index=labels)
print("Number of student whoes percentage more than 70:")
print(df[df['perc'] > 70])

OUTPUT:

Number of student whoes percentage more than 70:

name perc qualify

A Aman 79.5 yes

C Amjad 90.5 yes

J Pooja 89.0 yes


# Write a Pandas program to select the rows the percentage is between
70 and 90 (inclusive)
import pandas as pd
import numpy as np

exam_data = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit',


'Sumit', 'Matthew', 'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes',
'no', 'no', 'yes']}
labels = ['A', 'B', 'C', 'B', 'E', 'F', 'G', 'H', 'I', 'J']

df = pd.DataFrame(exam_data , index=labels)
print("Number of student whoes percentage more than 70:")
print(df[df['perc'].between(70,90)])

OUTPUT:

Number of student whoes percentage more than 70

name perc qualify

A Aman 79.5 yes

J Pooja 89.0 yes


# Write a Pandas program to change the percentage in given row by user.
import pandas as pd
import numpy as np

exam_dic = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit',


'Sumit', 'Matthew', 'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes',
'no', 'no', 'yes']}
labels = ['A', 'B', 'C', 'B', 'E', 'F', 'G', 'H', 'I', 'J']

df = pd.DataFrame(exam_dic , index=labels)
print("\nOriginal data frame:")
print(df)
ch = input("Enter the index of row : ")
per = float(input("Enter percentage to be changed: "))
print('\nChange the percentage in row '+ch+ ' to',per)
df.loc[ch, 'perc'] = per
print(df)

OUTPUT:

Original data frame:

name perc qualify

A Aman 79.5 yes

B Kamal 29.0 no

C Amjad 90.5 yes

B Rohan NaN no

E Amit 32.0 no

F Sumit 65.0 yes

G Matthew 56.0 yes

H Kartik NaN no

I Kavita 29.0 no

J Pooja 89.0 yes

Enter the index of row : 7

Enter percentage to be changed: 40


Cha

nge the percentage in row 7 to 40.0

name perc qualify

A Aman 79.5 yes

B Kamal 29.0 no

C Amjad 90.5 yes

B Rohan NaN no

E Amit 32.0 no

F Sumit 65.0 yes

G Matthew 56.0 yes

H Kartik NaN no

I Kavita 29.0 no

J Pooja 89.0 yes

7 NaN 40.0 NaN


# Write a Pandas program to join the two given dataframes along rows
and assign all data.
import pandas as pd
import numpy as np

exam_dic1 = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit',


'Sumit', 'Matthew', 'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes',
'no', 'no', 'yes']}

exam_data1 = pd.DataFrame(exam_dic1)

exam_dic2 = {'name': ['Parveen', 'Ahil', 'Ashaz', 'Shifin', 'Hanash'],


'perc': [89.5, 92, 90.5, 91.5, 90],
'qualify': ['yes', 'yes', 'yes', 'yes', 'yes']}

exam_data2 = pd.DataFrame(exam_dic2)

print("Original DataFrames:")
print(exam_data1)
print("-------------------------------------")
print(exam_data2)
print("\nJoin the said two dataframes along rows:")
result_data = pd.concat([exam_data1, exam_data2])
print(result_data)

OUTPUT:

Join the said two dataframes along rows:

name perc qualify

0 Aman 79.5 yes

1 Kamal 29.0 no

2 Amjad 90.5 yes

3 Rohan NaN no

4 Amit 32.0 no

5 Sumit 65.0 yes

6 Matthew 56.0 yes


7 Kartik NaN no

8 Kavita 29.0 no
9 Pooja 89.0 yes
# Write a Pandas program to join the two given dataframes along columns
and assign all data.
import pandas as pd
import numpy as np

exam_dic1 = {'name': ['Aman', 'Kamal', 'Amjad', 'Rohan', 'Amit',


'Sumit', 'Matthew', 'Kartik', 'Kavita', 'Pooja'],
'perc': [79.5, 29, 90.5, np.nan, 32, 65, 56, np.nan, 29, 89],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes',
'no', 'no', 'yes']}

exam_data1 = pd.DataFrame(exam_dic1)

exam_dic2 = {'name': ['Parveen', 'Ahil', 'Ashaz', 'Shifin', 'Hanash'],


'perc': [89.5, 92, 90.5, 91.5, 90],
'qualify': ['yes', 'yes', 'yes', 'yes', 'yes']}

exam_data2 = pd.DataFrame(exam_dic2)

print("Original DataFrames:")
print(exam_data1)
print("-------------------------------------")
print(exam_data2)
print("\nJoin the said two dataframes along rows:")
result_data = pd.concat([exam_data1, exam_data2],axis=1)
print(result_data)

OUTPUT:

Join the said two dataframes along rows:

name perc qualify name perc qualify

0 Aman 79.5 yes Parveen 89.5 yes

1 Kamal 29.0 no Ahil 92.0 yes

2 Amjad 90.5 yes Ashaz 90.5 yes

3 Rohan NaN no Shifin 91.5 yes

5 Sumit 65.0 yes NaN NaN NaN

6 Matthew 56.0 yes NaN NaN NaN

7 Kartik NaN no NaN NaN NaN

8 Kavita 29.0 no NaN NaN NaN

9 Pooja 89.0 yes NaN NaN NaN

You might also like