1/23/24, 10:38 PM Python Coding Practice - by Amar Sharma - Jupyter Notebook
PYTHON Coding Practice
created by Amar Sharma
Import libraries
In [1]: import pandas as pd
import numpy as np
Creating DataFrame
In [2]: a = {"door":(5,3,4,7,2),"window":(2,5,7,8,6)}
b = pd.DataFrame(a)
b
Out[2]:
door window
0 5 2
1 3 5
2 4 7
3 7 8
4 2 6
In [3]: c = {"price":(12,15,72,64,52)}
d = pd.DataFrame(c)
d
Out[3]:
price
0 12
1 15
2 72
3 64
4 52
Joining DataFrames
pd.concat()
127.0.0.1:8889/notebooks/Python Coding Practice - by Amar Sharma.ipynb 1/9
1/23/24, 10:38 PM Python Coding Practice - by Amar Sharma - Jupyter Notebook
In [4]: e = pd.concat([b,d],axis = 1)
e
Out[4]:
door window price
0 5 2 12
1 3 5 15
2 4 7 72
3 7 8 64
4 2 6 52
first few rows of your DataFrame
In [5]: e.head()
Out[5]:
door window price
0 5 2 12
1 3 5 15
2 4 7 72
3 7 8 64
4 2 6 52
last few rows of your DataFrame
In [6]: e.tail()
Out[6]:
door window price
0 5 2 12
1 3 5 15
2 4 7 72
3 7 8 64
4 2 6 52
127.0.0.1:8889/notebooks/Python Coding Practice - by Amar Sharma.ipynb 2/9
1/23/24, 10:38 PM Python Coding Practice - by Amar Sharma - Jupyter Notebook
descriptive statistics of a DataFrame
In [7]: e.describe()
Out[7]:
door window price
count 5.000000 5.000000 5.00000
mean 4.200000 5.600000 43.00000
std 1.923538 2.302173 27.87472
min 2.000000 2.000000 12.00000
25% 3.000000 5.000000 15.00000
50% 4.000000 6.000000 52.00000
75% 5.000000 7.000000 64.00000
max 7.000000 8.000000 72.00000
concise summary of a DataFrame, including information about the
data types
In [8]: e.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 door 5 non-null int64
1 window 5 non-null int64
2 price 5 non-null int64
dtypes: int64(3)
memory usage: 248.0 bytes
select column from your DataFrame
In [9]: e[['door']]
Out[9]:
door
0 5
1 3
2 4
3 7
4 2
127.0.0.1:8889/notebooks/Python Coding Practice - by Amar Sharma.ipynb 3/9
1/23/24, 10:38 PM Python Coding Practice - by Amar Sharma - Jupyter Notebook
In [10]: e[['door', 'price']]
Out[10]:
door price
0 5 12
1 3 15
2 4 72
3 7 64
4 2 52
Indexing and selecting data
In [11]: e
Out[11]:
door window price
0 5 2 12
1 3 5 15
2 4 7 72
3 7 8 64
4 2 6 52
In [12]: e.loc[2,"window"] # 2 = row index
Out[12]: 7
In [13]: e.iloc[2,1] # 2 = ROw index , 1 = column index
Out[13]: 7
Filtering rows based on condition
In [14]: e[e["price"]>20]
Out[14]:
door window price
2 4 7 72
3 7 8 64
4 2 6 52
127.0.0.1:8889/notebooks/Python Coding Practice - by Amar Sharma.ipynb 4/9
1/23/24, 10:38 PM Python Coding Practice - by Amar Sharma - Jupyter Notebook
Droping the column
In [15]: e
Out[15]:
door window price
0 5 2 12
1 3 5 15
2 4 7 72
3 7 8 64
4 2 6 52
In [16]: f = e.drop("window", axis = 1)
f
Out[16]:
door price
0 5 12
1 3 15
2 4 72
3 7 64
4 2 52
Creating DataFrame with None value
In [17]: g = {"window":(2,5,7,None,5)}
h=pd.DataFrame(g)
h
Out[17]:
window
0 2.0
1 5.0
2 7.0
3 NaN
4 5.0
127.0.0.1:8889/notebooks/Python Coding Practice - by Amar Sharma.ipynb 5/9
1/23/24, 10:38 PM Python Coding Practice - by Amar Sharma - Jupyter Notebook
Filled non value with a number
In [18]: h.fillna(3)
Out[18]:
window
0 2.0
1 5.0
2 7.0
3 3.0
4 5.0
Filled none value with mean, median, mode
In [19]: h
Out[19]:
window
0 2.0
1 5.0
2 7.0
3 NaN
4 5.0
In [20]: h.fillna(h["window"].mean())
Out[20]:
window
0 2.00
1 5.00
2 7.00
3 4.75
4 5.00
In [21]: h.fillna(h["window"].median())
Out[21]:
window
0 2.0
1 5.0
2 7.0
3 5.0
4 5.0
127.0.0.1:8889/notebooks/Python Coding Practice - by Amar Sharma.ipynb 6/9
1/23/24, 10:38 PM Python Coding Practice - by Amar Sharma - Jupyter Notebook
In [22]: h.fillna(h["window"].mode()[0])
Out[22]:
window
0 2.0
1 5.0
2 7.0
3 5.0
4 5.0
Adding New Column
In [23]: e
Out[23]:
door window price
0 5 2 12
1 3 5 15
2 4 7 72
3 7 8 64
4 2 6 52
In [24]: e["fan"]=[1,2,5,2,4]
e
Out[24]:
door window price fan
0 5 2 12 1
1 3 5 15 2
2 4 7 72 5
3 7 8 64 2
4 2 6 52 4
Arranging the position of column
In [25]: e[["door","window","fan","price"]]
Out[25]:
door window fan price
0 5 2 1 12
1 3 5 2 15
2 4 7 5 72
3 7 8 2 64
4 2 6 4 52
127.0.0.1:8889/notebooks/Python Coding Practice - by Amar Sharma.ipynb 7/9
1/23/24, 10:38 PM Python Coding Practice - by Amar Sharma - Jupyter Notebook
In [26]: e[[col for col in e.columns if col != "price"] + ["price"]]
Out[26]:
door window fan price
0 5 2 1 12
1 3 5 2 15
2 4 7 5 72
3 7 8 2 64
4 2 6 4 52
mean, median,mode column wise
In [27]: e
Out[27]:
door window price fan
0 5 2 12 1
1 3 5 15 2
2 4 7 72 5
3 7 8 64 2
4 2 6 52 4
In [28]: e.mean()
Out[28]: door 4.2
window 5.6
price 43.0
fan 2.8
dtype: float64
In [29]: e.median()
Out[29]: door 4.0
window 6.0
price 52.0
fan 2.0
dtype: float64
In [30]: e.mode().iloc[0]
Out[30]: door 2.0
window 2.0
price 12.0
fan 2.0
Name: 0, dtype: float64
127.0.0.1:8889/notebooks/Python Coding Practice - by Amar Sharma.ipynb 8/9
1/23/24, 10:38 PM Python Coding Practice - by Amar Sharma - Jupyter Notebook
Variance & standard deviation
In [31]: e.var()
Out[31]: door 3.7
window 5.3
price 777.0
fan 2.7
dtype: float64
In [32]: e.std()
Out[32]: door 1.923538
window 2.302173
price 27.874720
fan 1.643168
dtype: float64
Correlation & Covariance
In [33]: e.corr()
Out[33]:
door window price fan
door 1.000000 0.191946 0.195829 -0.458760
window 0.191946 1.000000 0.868753 0.568354
price 0.195829 0.868753 1.000000 0.742312
fan -0.458760 0.568354 0.742312 1.000000
In [34]: e.cov()
Out[34]:
door window price fan
door 3.70 0.85 10.50 -1.45
window 0.85 5.30 55.75 2.15
price 10.50 55.75 777.00 34.00
fan -1.45 2.15 34.00 2.70
need more like this ?
127.0.0.1:8889/notebooks/Python Coding Practice - by Amar Sharma.ipynb 9/9