Session-24 - Jupyter Notebook
Session-24 - Jupyter Notebook
In [2]: 1 df=pd.read_csv("Iris.csv")
2 df
Out[2]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
Out[3]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
Out[4]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
localhost:8888/notebooks/Desktop/Techpaathsala/Session-24.ipynb 1/13
3/12/24, 10:13 PM Session-24 - Jupyter Notebook
In [5]: 1 df.head(1)
Out[5]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
In [6]: 1 df.head(2)
Out[6]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
In [7]: 1 df.tail(1)
Out[7]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
In [8]: 1 df.tail(2)
Out[8]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
In [9]: 1 df.tail(8)
Out[9]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
localhost:8888/notebooks/Desktop/Techpaathsala/Session-24.ipynb 2/13
3/12/24, 10:13 PM Session-24 - Jupyter Notebook
In [11]: 1 df=pd.read_csv("Iris.csv")
2 df
Out[11]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
Out[12]:
A B C D E F
localhost:8888/notebooks/Desktop/Techpaathsala/Session-24.ipynb 3/13
3/12/24, 10:13 PM Session-24 - Jupyter Notebook
In [14]: 1 # transpose
2
3 df.T
Out[14]:
0 1 2 3 4 5 6 7 8 9 ... 14
A 1 2 3 4 5 6 7 8 9 10 ... 14
B 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 ... 6
C 3.5 3.0 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... 3
D 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... 5
E 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... 2
Iris- Iris- Iris- Iris- Iris- Iris- Iris- Iris- Iris- Iris- Iri
F ...
setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa virginic
Out[15]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
localhost:8888/notebooks/Desktop/Techpaathsala/Session-24.ipynb 4/13
3/12/24, 10:13 PM Session-24 - Jupyter Notebook
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Id 150 non-null int64
1 SepalLengthCm 150 non-null float64
2 SepalWidthCm 150 non-null float64
3 PetalLengthCm 150 non-null float64
4 PetalWidthCm 150 non-null float64
5 Species 150 non-null object
dtypes: float64(4), int64(1), object(1)
memory usage: 7.2+ KB
Out[19]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm
Out[20]: (150, 6)
Out[21]: 150
localhost:8888/notebooks/Desktop/Techpaathsala/Session-24.ipynb 5/13
3/12/24, 10:13 PM Session-24 - Jupyter Notebook
Out[22]: 6
DataFrame:-
In [23]: 1 # 1. Create DataFrame:-
2
3 import pandas as pd
4 import numpy as np
In [24]: 1 df=pd.DataFrame()
2 df
Out[24]:
Out[27]:
0 1 2 3 4
0 2 3 4 7 6
In [26]: 1 list1=[2,3,4,5,6]
2 list2=[100,200,300,400,500]
3
4 df=pd.DataFrame([list1,list2])
5 df
Out[26]:
0 1 2 3 4
0 2 3 4 5 6
localhost:8888/notebooks/Desktop/Techpaathsala/Session-24.ipynb 6/13
3/12/24, 10:13 PM Session-24 - Jupyter Notebook
In [29]: 1
2 df=pd.DataFrame(arr1)
3 df
Out[29]:
0
0 1
1 2
2 3
3 4
4 5
Out[30]: {'Randint': array([17, 18, 19, 16, 10, 10, 12, 16, 14, 19]),
'Arange': array([11, 13, 15, 17, 19, 21, 23, 25, 27, 29])}
In [31]: 1 df=pd.DataFrame(dict1)
2 df
Out[31]:
Randint Arange
0 17 11
1 18 13
2 19 15
3 16 17
4 10 19
5 10 21
6 12 23
7 16 25
8 14 27
9 19 29
In [ ]: 1 # first last
2 # 0 krishna rai
3 # 1 Shubham Singh
4 # 2 rahul raj
In [32]: 1 data={'First_name':['Yash','Kimaya','Krishna'],
2 'Last_name' : ['Patil','Churi','Rai']}
3 data
localhost:8888/notebooks/Desktop/Techpaathsala/Session-24.ipynb 7/13
3/12/24, 10:13 PM Session-24 - Jupyter Notebook
In [33]: 1 df=pd.DataFrame(data)
2 df
Out[33]:
First_name Last_name
0 Yash Patil
1 Kimaya Churi
2 Krishna Rai
In [34]: 1 l1 = ["krishna","shubham","rahul"]
2 l2 = ["rai","singh","raj"]
3 data = {"FIRST NAME":l1,
4 "LAST NAME":l2}
5 df = pd.DataFrame(data)
6 df
Out[34]:
FIRST NAME LAST NAME
0 krishna rai
1 shubham singh
2 rahul raj
In [35]: 1 First=['Krishna','shubham','rahul']
2 Last=['Rai', 'Singh', 'Raj']
3 df=pd.DataFrame([First,Last])
4 df
Out[35]:
0 1 2
In [36]: 1 df=pd.DataFrame([[1,2],[3,4]])
2 df
Out[36]:
0 1
0 1 2
1 3 4
In [37]: 1 df=pd.DataFrame([[1,2],[3,4]],columns=['X','Y'],index=['P','Q'])
2 df
Out[37]:
X Y
P 1 2
Q 3 4
localhost:8888/notebooks/Desktop/Techpaathsala/Session-24.ipynb 8/13
3/12/24, 10:13 PM Session-24 - Jupyter Notebook
In [38]: 1 df=pd.DataFrame([[1,2],[3,4]],columns=['col1','col2'],index=['row1',
2 df
Out[38]:
col1 col2
row1 1 2
row2 3 4
Out[39]:
col1 col2 col3
row1 1 2 100
row2 3 4 200
Out[40]:
col1 col2
row1 1 2
row2 3 4
In [43]: 1 df2=pd.DataFrame([[100,200],[300,400]],columns=['col1','col2'],index
2 df2
Out[43]:
col1 col2
In [53]: 1 df=pd.concat([df1,df2],axis=0)
2 df
Out[53]:
col1 col2
row1 1 2
row2 3 4
localhost:8888/notebooks/Desktop/Techpaathsala/Session-24.ipynb 9/13
3/12/24, 10:13 PM Session-24 - Jupyter Notebook
In [54]: 1 df=pd.concat([df1,df2],axis=0)
2 df
Out[54]:
col1 col2 col1 col2
In [56]: 1 df2=pd.DataFrame([[100,200],[300,400]],columns=['col1','col2'],index
2 display(df2)
3 display(df1)
col1 col2
col1 col2
row1 1 2
row2 3 4
In [57]: 1 df=pd.concat([df1,df2],axis=1)
2 df
Out[57]:
col1 col2 col1 col2
In [65]: 1 # df1
2 df2
Out[65]:
col1 col2
localhost:8888/notebooks/Desktop/Techpaathsala/Session-24.ipynb 10/13
3/12/24, 10:13 PM Session-24 - Jupyter Notebook
In [67]: 1 # df1=pd.DataFrame([[10,20],[100,200]])
2 # df2=pd.DataFrame([[100,200],[23,56]])
3 # df1.append(df2)
-----------------------------------------------------------------------
----
AttributeError Traceback (most recent call l
ast)
~\AppData\Local\Temp\ipykernel_22608\400476097.py in ?()
1 df1=pd.DataFrame([[10,20],[100,200]])
2 df2=pd.DataFrame([[100,200],[23,56]])
----> 3 df1.append(df2)
In [68]: 1 df=pd.read_csv('Iris.csv')
2 df
Out[68]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
localhost:8888/notebooks/Desktop/Techpaathsala/Session-24.ipynb 11/13
3/12/24, 10:13 PM Session-24 - Jupyter Notebook
In [69]: 1 # 1. Slicing
2 # Access rows
3 df[1:10]
Out[69]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species
Out[91]:
Id SepalLengthCm Species
0 1 5.1 Iris-setosa
1 2 4.9 Iris-setosa
2 3 4.7 Iris-setosa
3 4 4.6 Iris-setosa
4 5 5.0 Iris-setosa
1 loc
2 iloc
3 groupby
4 get_group
5 join
6 merge
7 replace
8 Apply
9 null value
10 unique value
11 columns_reset
12 inplace
localhost:8888/notebooks/Desktop/Techpaathsala/Session-24.ipynb 12/13
3/12/24, 10:13 PM Session-24 - Jupyter Notebook
13 Insert
In [ ]: 1 df.isna().sum()
2 df.fillna(mean,medium,mode)
localhost:8888/notebooks/Desktop/Techpaathsala/Session-24.ipynb 13/13