Pandas Questions Ip File
Pandas Questions Ip File
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
ds = ds1 + ds2
print(ds)
ds = ds1 - ds2
print(ds)
ds = ds1 * ds2
print(ds)
ds = ds1 / ds2
print(ds)
Output:
import pandas as pd
new_series = pd.Series(d1)
print(new_series)
Output:
new_series:
a 100
b 200
c 300
d 400
e 800
dtype: int64
import numpy as np
import pandas as pd
ser = pd.Series(np_array)
print(ser)
Output:
0 10
1 20
2 30
3 40
4 50
dtype: int64
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
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:
Python Code :
import pandas as pd
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
Python Code:
import pandas as pd
print(s)
print(new_s)
Output:
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:
b Dima 9.0 3 no
e Emily 9.0 2 no
Python code:
import pandas as pd
print(df.head(3))
Output:
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
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:
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:
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:
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.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]
plt.scatter(x, y, c ="magenta",marker='^')
plt.show()
Output:s