Practicals
Practicals
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:
0 5 0 6
1 10 1 24
2 15 2 54
3 20 3 96
4 25 4 150
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
df = pd.DataFrame(exam_data , index=labels)
print("Number of student whoes percentage more than 70:")
print(df[df['perc'] > 70])
OUTPUT:
df = pd.DataFrame(exam_data , index=labels)
print("Number of student whoes percentage more than 70:")
print(df[df['perc'].between(70,90)])
OUTPUT:
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:
B Kamal 29.0 no
B Rohan NaN no
E Amit 32.0 no
H Kartik NaN no
I Kavita 29.0 no
B Kamal 29.0 no
B Rohan NaN no
E Amit 32.0 no
H Kartik NaN no
I Kavita 29.0 no
exam_data1 = pd.DataFrame(exam_dic1)
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:
1 Kamal 29.0 no
3 Rohan NaN no
4 Amit 32.0 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_data1 = pd.DataFrame(exam_dic1)
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: