PRACTICAL FILE IP - Copy (1)
PRACTICAL FILE IP - Copy (1)
individual strings.
CODE:
import pandas as pd
ser = pd.Series(["Teacher","No","1"])
print(ser)
OUTPUT:
0 Teacher
1 No
2 1
dtype: object
#2 Write a program to create a Series using ndarray
having 5 elements from 22 to 48 and also tiling it
twice.
CODE:
import pandas as pd
import numpy as np
list = np.linspace(22,35,5)
ser = pd.Series(np.tile(list,2))
print(ser)
OUTPUT:
0 22.00
1 25.25
2 28.50
3 31.75
4 35.00
5 22.00
6 25.25
7 28.50
8 31.75
9 35.00
dtype: float64
OUTPUT:
2020 150
2022 200
2024 180
2026 195
2028 210
dtype: int64
CODE:
import pandas as pd
ser = pd.Series([3,6,9,12,15,18,21,24,27])
print(ser.head(7))
print(ser.tail())
OUTPUT:
0 3 4 15
1 6 5 18
2 9 6 21
3 12 7 24
4 15 8 27
5 18 dtype: int64
6 21
dtype: int64
CODE:
import pandas as pd
list = [[25,34,88],[45,90,67],[56,60,89]]
df =
pd.DataFrame(list,index=["row1","row2","row3"])
print(df)
OUTPUT:
0 1 2
row1 25 34 88
row2 45 90 67
row3 56 60 89
CODE:
import pandas as pd
ser = pd.Series([5,3,12,9,14])
ser2 = ser**2
ser3 = ser*2
print(ser)
print(ser2)
print(ser3)
OUTPUT:
0 5 0 25 0 10
1 3 1 9 1 6
2 12 2 144 2 24
3 9 3 81 3 18
4 14 4 196 4 28
dtype: int64 dtype: int64 dtype: int64
OUTPUT:
OUTPUT:
Row index: Qty1 Row index: Qty3
Containig: Containig:
yr1 34500 yr1 47000
yr2 44900 yr2 57000
yr3 54500 yr3 57000
Name: Qty1, dtype: int64 Name: Qty3, dtype: int64
Row index: Qty2 Row index: Qty4
Containig: Containig:
yr1 56000 yr1 49000
yr2 46100 yr2 59000
yr3 51000 yr3 58500
Name: Qty2, dtype: int64 Name: Qty4, dtype: int64
OUTPUT:
A B C A B C
0 101.0 302.0 0 -99.0 -298.0 -497.0
503.0 1 -196.0 -394.0 -594.0
1 204.0 406.0 606.0 2 NaN NaN NaN
2 NaN NaN NaN
A B C A B C
0 101.0 302.0 0 99.0 298.0 497.0
503.0 1 196.0 394.0 594.0
1 204.0 406.0 2 NaN NaN NaN
606.0
2 NaN NaN NaN
OUTPUT:
A B C A B C
0 10000 250000 NaN 0 0.01 0.010000 NaN
1 80000 80000 NaN 1 0.02 0.005000 NaN
2 90000 420000 NaN 2 0.01 0.011667 NaN
A B C
0 100.0 100.000000 NaN
1 50.0 200.000000 NaN
#13 Create a demo DataFrame for environment and
use functions min(),max().
CODE:
import pandas as pd
data = {"Flora": [1200, 900, 1500, 600,
800],"Fauna": [450, 700, 600, 300, 550],"Natural
Reserves": [15, 10, 20, 5, 8]}
countries = ["India", "Australia", "Brazil", "Canada",
"South Africa"]
ent_df = pd.DataFrame(data, index=countries)
print(ent_df)
print(ent_df.min())
print(ent_df.max())
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
#18 Create
a line
graph
showing
graph for Sin(x), Cos(x) and Tan(x) for each of the
following. Give suitable title and legend.
CODE:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0.,10,0.1)
a = np.sin(x)
b = np.cos(x)
c = np.tan(x)
plt.figure(figsize=(8, 4))
plt.plot(x,a,color='b',linestyle='dashed',linewidth=2)
plt.plot(x,b,color='r',linestyle='dotted',linewidth=2)
plt.plot(x,c,color='g',linestyle='dashdot')
plt.title("Wave")
plt.xlabel("x")
plt.ylabel("Function")
plt.legend()
plt.ylim(-2,2)
plt.show()
OUTPUT:
#19 Create a scatter chart for random values.
CODE:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.randint(1,100,size=(300,))
y = np.random.randint(1,100,size=(300,))
color = ['r','b','c','m','g','k']*(len(x)//6)
plt.scatter(x,y,c=color)
plt.title("Scatter Values View")
plt.xlabel("X values")
plt.ylabel("Y label")
plt.show()
OUTPUT:
#20 Create a horizontal bar chart showing heights of 6
different persons.
CODE:
import matplotlib.pyplot as plt
heights = [5.2,6,5.10,5.5,5,6.2]
names =
['Aarav','Ananya','Kavya','Ishaan','Priya','Veer']
plt.barh(names,heights,color=['r','b','c','m','g','y'])
plt.title("Height")
plt.xlabel("Heights")
plt.ylabel("Names")
plt.xlim(4.5,6.5)
plt.show()
OUTPUT:
#21 Consider a TSS school collection by 6
sections(A,B,C,D,E,F). Create a pie chart showing the
collection percentage wise. Also explode A,C,D
sections.
CODE:
import matplotlib.pyplot as plt
col = [9900,15500,11300,7900,18600,8300]
sec = ['A','B','C','D','E','F']
exp = [0.2,0,0.1,0.3,0,0]
plt.title("Collection By Sections")
plt.axis("equal")
plt.pie(col,labels=sec,explode=exp,colors=['cyan','go
ld','violet','lightgreen','pink','silver'])
plt.show()
OUTPUT:
#22
Import
CSV
text
file
from
desktop as reference. Also print it without header and
with column indexes as EmpNo.
CODE:
import pandas as pd
df1 = pd.read_csv("C:\\Users\\blsro\\OneDrive\\
Desktop\\Employee.txt")
print(df1)
df2 = pd.read_csv("C:\\Users\\blsro\\OneDrive\\
Desktop\\Employee.txt",header=None,skiprows=1)
print(df2)
df3 = pd.read_csv("C:\\Users\\blsro\\OneDrive\\
Desktop\\Employee.txt",index_col="EmpNo ")
print(df3)
OUTPUT:
0 1 2 3
#23 Create a Dataframe and save it using a csv file.
CODE:
import numpy as np
import pandas as pd
a = {"Name":
['Purv','Paschim','Dakshin','Uttar','Kendriya','Rural'],"P
roduct":
['Oven','AC','AC','Oven','TV','Tubewell'],"Target":
[56000.00,70000.00,np.NaN,75000.00,60000.00,np.
NaN],"Sales":
[58000.00,68000.00,np.NaN,78000.00,61000.00,np.
NaN]}
adf =
pd.DataFrame(a,index=['SecA','SecB','SecC','SecD','S
ecE','SecF'])
print(adf)
adf.to_csv("C:\\Users\\blsro\\OneDrive\\Desktop\\
Sales.txt")
OUTPUT:
mycon.is_connected():
print('connected successfully')
bdf = pd.read_sql("select * from contact;",mycon)
print(bdf)
OUTPUT:
connected successfully
contact_id name phone
email
0 C001 Rajesh Kumar 9876543210
[email protected]
1 C002 Anjali Sharma 8765432109
[email protected]
2 C003 Mohammed Iqbal 7654321098
[email protected]
3 C004 Priya Reddy None
[email protected]
4 C005 Amit Verma 9123456789
[email protected]
OUTPUT:
connected successfully
Enter the book_id:101
Enter the Title of the Book:Dune
Enter the Genre:Science-Fiction
Enter the Publication Year:1965
Record Added Successfully