Q1.
Write a Pandas program to create and display a one-dimensional array-like
object containing an array of data.
Python Code :
import pandas as pd
ds = pd.Series([2, 4, 6, 8, 10])
print(ds)
Output:
0 2
1 4
2 6
3 8
4 10
dtype: int64
Q2.Write a Pandas program to add, subtract, multiple and divide two Pandas Series.
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)
Output:
Add two Series:
0 3
1 7
2 11
3 15
4 19
dtype: int64
Subtract two Series:
0 1
1 1
2 1
3 1
4 1
dtype: int64
Multiply two Series:
0 2
1 12
2 30
3 56
4 90
dtype: int64
Divide Series1 by Series2:
0 2.000000
1 1.333333
2 1.200000
3 1.142857
4 1.111111
dtype: fl
Q3.Write a Pandas program to convert a dictionary to a Pandas series:
{'a': 100, 'b': 200, 'c': 300, 'd': 400, 'e': 800}
Python Code :
import pandas as pd
d1 = {'a': 100, 'b': 200, 'c':300, 'd':400, 'e':800}
new_series = pd.Series(d1)
print(new_series)
Output:
new_series:
a 100
b 200
c 300
d 400
e 800
dtype: int64
Q4. Write a Pandas program to convert a NumPy array to a Pandas series.
Sample NumPy array: d1 = [10, 20, 30, 40, 50]
Python Code :
import numpy as np
import pandas as pd
np_array = np.array([10, 20, 30, 40, 50])
ser = pd.Series(np_array)
print(ser)
Output:
0 10
1 20
2 30
3 40
4 50
dtype: int64
Q5.Write a Pandas program to convert the first column of a DataFrame as a Series.
col1 col2 col3
0 1 4 7
1 2 5 5
2 3 6 8
3 4 9 12
4 7 5 1
5 1 10 11
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(d)
print(df['col1'])
Output:
col1
0 1
1 2
2 3
3 4
4 7
5 11
Name: col1, dtype: int64
Q6.Write a Pandas program to convert a given Series to an array.
Python Code :
import pandas as pd
import numpy as np
ser = pd.Series(['100', '200', 'python', '300.12', '400'])
s1=np.array(ser)
print(s1)
Output:
['100' '200' 'python' '300.12' '400']
Q7. Write a Pandas program to sort the given values.
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)
Output:
Original Data Series:
0 100
1 200
2 python
3 300.12
4 400
dtype: object
0 100
1 200
3 300.12
4 400
2 python
dtype: object
Q8. Write a Pandas program to add some data to an existing Series.
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)
Output:
Original Data Series:
0 100
1 200
2 python
3 300.12
4 400
dtype: object
Series after adding some data:
Data 0 100
1 200
2 python
3 300.12
4 400
5 500
6 php
Q9. Import pandas and print its version.
Python code:
import pandas as pd
print(pd.__version__)
Output:
1.4.4
Q10. Write a Pandas program to get the powers of an array values element-wise.
Note: First array elements raised to powers from second array
Sample data: {'X':[78,85,96,80,86], 'Y':[84,94,89,83,86],'Z':[86,97,96,72,83]}
Python Code :
import pandas as pd
df = pd.DataFrame({'X':[78,85,96,80,86], 'Y':[84,94,89,83,86],'Z':
[86,97,96,72,83]});
print(df)
Output:
X Y Z
0 78 84 86
1 85 94 97
2 96 89 96
3 80 83 72
4 86 86 83
Q11. Write a Pandas program to get the first 3 rows of a given DataFrame:
name score attempts qualify
a Anastasia 12.5 1 yes
b Dima 9.0 3 no
c Katherine 16.5 2 yes
d James NaN 3 no
e Emily 9.0 2 no
Python code:
import pandas as pd
print(df.head(3))
Output:
attempts name qualify score
a 1 Anastasia yes 12.5
b 3 Dima no 9.0
c 2 Katherine yes 16.5
Q12. Write a Pandas program to select the 'name' and 'score' columns from the
following DataFrame. : exam_data = {'name': ['Anastasia', 'Dima', 'Katherine',
'James', 'Emily'],
'score': [12.5, 9, 16.5, np.nan, 9],
'attempts': [1, 3, 2, 3, 2],
'qualify': ['yes', 'no', 'yes', 'no', 'no']}
labels = ['a', 'b', 'c', 'd', 'e']
Python Code :
import pandas as pd
import numpy as np
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James',
'Emily'],
'score': [12.5, 9, 16.5, np.nan, 9],
'attempts': [1, 3, 2, 3, 2],
'qualify': ['yes', 'no', 'yes', 'no', 'no']}
labels = ['a', 'b', 'c', 'd', 'e']
df = pd.DataFrame(exam_data , index=labels)
print(df[['name', 'score']])
Output:
name score
a Anastasia 12.5
b Dima 9.0
c Katherine 16.5
d James NaN
e Emily 9.0
Q13. Write a Pandas program to select the rows where the number of attempts in
the examination is greater than 2. (same sample df)
Python code:
import pandas as pd
print(df[df['attempts'] > 2])
Output:
name score attempts qualify
b Dima 9.0 3 no
d James NaN 3 no
Q14.Write a Pandas program to count the number of rows and columns of a
DataFrame(same sample df)
Python code:
Import pandas as pd
print("Number of Rows: "+str(total_rows))
print("Number of Columns: "+str(total_cols))
Output:
Number of Rows: 5
Number of Columns: 4
Q15. Write a Pandas program to select the rows the score is between 15 and 20
(same sample df)
Python code:
Import pandas as pd
print(df[df['score'].between(15, 20)])
Output:
attempts name qualify score
c 2 Katherine yes 16.5
MATPLOTLIB PLOTTING
Q1. Draw a line in a diagram from position (1, 3) to position (8, 10):
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1, 8])
ypoints = np.array([3, 10])
plt.plot(xpoints, ypoints)
plt.show()
Output:
Q2. Plot the following points on the graph: 3,8,1,10
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker = 'o')
plt.show()
Output:
Q3.Add grid lines and labels to the graph.
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
plt.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x, y)
plt.grid()
plt.show()
Output:
Q4.Plot a bar chart.
import matplotlib.pyplot as plt
Country=['USA','Canada','Germany','UK ', 'France']
GDP_per_Capita=[45000,42000,52000,49000,47000]
New_Colors = ['green','blue','purple','brown','teal']
plt.bar(Country, GDP_per_Capita, color=New_Colors)
plt.title('Country vs GDP per Capita')
plt.xlabel('Country')
plt.ylabel('GDP per Capita')
plt.grid(True)
plt.show()
Output:
Q5.Plot a scatter chart.
import matplotlib.pyplot as plt.scatter
x =[5, 7, 8, 7, 2, 17, 2, 9,
4, 11, 12, 9, 6]
y =[99, 86, 87, 88, 100, 86,
103, 87, 94, 78, 77, 85, 86]
plt.scatter(x, y, c ="magenta",marker='^')
plt.show()
Output:s