Data - Analysis (Intro To Pandas)
Data - Analysis (Intro To Pandas)
Funtions in Python
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into
a function. A function can return data as a result.
In [1]:
1 def num():
2 return 'hello world!'
In [19]:
In [17]:
1 def car_sale(car_repo):
2 user_name = input("enter your name: ")
3 choice = input("what car do you want: ")
4 for choice in car_repo.keys():
5 if car == choice:
6 return f"{user_name} we have {choice} details: {car_data[car]}"
In [18]:
1 car_sale(car_data)
Out[18]:
In [ ]:
In [2]:
1 num()
Out[2]:
'hello world!'
localhost:8889/notebooks/Desktop/C4LEB/Desktop/python_class/Day_2_uni.ipynb 1/16
03/10/2023, 01:54 Day_2_uni - Jupyter Notebook
In [3]:
1 def add(x):
2 return 2+x
In [4]:
1 add(3)
Out[4]:
In [5]:
1 import math as m
In [6]:
1 m.sqrt(4)
Out[6]:
2.0
Introduction to
numpy
pandas
matplotlib
Data Understanding using the above modules
In [1]:
1 # importing libraries
2 import pandas as pd
In [25]:
1 house = {"door_type":[1,2,3,4,5,6,12,8,9,0],
2 "light":[6,5,4,34,11,1,23,45,67,89],
3 "roof": ['high','low','mid','cid','kee', 'u','me','nine', 'ten', 'comp
4 'price': [100,200,300,400,500,600,700,800,900,1000]}
In [26]:
localhost:8889/notebooks/Desktop/C4LEB/Desktop/python_class/Day_2_uni.ipynb 2/16
03/10/2023, 01:54 Day_2_uni - Jupyter Notebook
In [27]:
1 data
Out[27]:
0 1 6 high 100
1 2 5 low 200
2 3 4 mid 300
3 4 34 cid 400
4 5 11 kee 500
5 6 1 u 600
6 12 23 me 700
7 8 45 nine 800
8 9 67 ten 900
9 0 89 complet 1000
In [7]:
1 data.head(10)
Out[7]:
0 1 6 high 100
1 2 5 low 200
2 3 4 mid 300
3 4 34 cid 400
4 5 11 kee 500
5 6 1 u 600
6 12 23 me 700
7 8 45 nine 800
8 9 67 ten 900
9 0 89 complet 1000
In [30]:
1 data.tail(2)
Out[30]:
8 9 67 ten 900
9 0 89 complet 1000
localhost:8889/notebooks/Desktop/C4LEB/Desktop/python_class/Day_2_uni.ipynb 3/16
03/10/2023, 01:54 Day_2_uni - Jupyter Notebook
In [31]:
1 data.shape #to get the nunmber of rows and columns in the dataset
Out[31]:
(10, 4)
In [33]:
1 data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 door_type 10 non-null int64
1 light 10 non-null int64
2 roof 10 non-null object
3 price 10 non-null int64
dtypes: int64(3), object(1)
memory usage: 448.0+ bytes
In [34]:
1 data.describe()
Out[34]:
In [31]:
1 a =3
2 a = 5
3 print(a)
localhost:8889/notebooks/Desktop/C4LEB/Desktop/python_class/Day_2_uni.ipynb 4/16
03/10/2023, 01:54 Day_2_uni - Jupyter Notebook
In [33]:
Out[33]:
0 1 6 100
1 2 5 200
2 3 4 300
3 4 34 400
4 5 11 500
5 6 1 600
6 12 23 700
7 8 45 800
8 9 67 900
9 0 89 1000
In [34]:
In [ ]:
In [66]:
1 x
Out[66]:
light
0 6
1 5
2 4
3 34
4 11
5 1
6 23
7 45
8 67
9 89
localhost:8889/notebooks/Desktop/C4LEB/Desktop/python_class/Day_2_uni.ipynb 5/16
03/10/2023, 01:54 Day_2_uni - Jupyter Notebook
In [58]:
1 data[["price", "door_type"]].describe()
Out[58]:
price door_type
In [62]:
1 type(data.price.values)
Out[62]:
numpy.ndarray
In [ ]:
In [ ]:
In [ ]:
In [ ]:
localhost:8889/notebooks/Desktop/C4LEB/Desktop/python_class/Day_2_uni.ipynb 6/16
03/10/2023, 01:54 Day_2_uni - Jupyter Notebook
In [35]:
1 data
Out[35]:
0 1 6 100
1 2 5 200
2 3 4 300
3 4 34 400
4 5 11 500
5 6 1 600
6 12 23 700
7 8 45 800
8 9 67 900
9 0 89 1000
In [8]:
Out[8]:
0 1 high 100
1 2 low 200
2 3 mid 300
3 4 cid 400
In [7]:
Out[7]:
5 6 u 600
6 12 me 700
7 8 nine 800
8 9 ten 900
9 0 complet 1000
localhost:8889/notebooks/Desktop/C4LEB/Desktop/python_class/Day_2_uni.ipynb 7/16
03/10/2023, 01:54 Day_2_uni - Jupyter Notebook
In [19]:
Out[19]:
(10, 4)
In [20]:
1 data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 door_type 10 non-null int64
1 light 10 non-null int64
2 roof 10 non-null object
3 price 10 non-null int64
dtypes: int64(3), object(1)
memory usage: 448.0+ bytes
In [21]:
Out[21]:
In [37]:
1 data.columns
Out[37]:
localhost:8889/notebooks/Desktop/C4LEB/Desktop/python_class/Day_2_uni.ipynb 8/16
03/10/2023, 01:54 Day_2_uni - Jupyter Notebook
In [38]:
1 x = list(data.columns)
2 print(x)
In [44]:
In [45]:
1 data.columns = x
In [46]:
1 data
Out[46]:
0 1 6 100
1 2 5 200
2 3 4 300
3 4 34 400
4 5 11 500
5 6 1 600
6 12 23 700
7 8 45 800
8 9 67 900
9 0 89 1000
localhost:8889/notebooks/Desktop/C4LEB/Desktop/python_class/Day_2_uni.ipynb 9/16
03/10/2023, 01:54 Day_2_uni - Jupyter Notebook
In [53]:
1 data[["door types"]]
Out[53]:
door types
0 1
1 2
2 3
3 4
4 5
5 6
6 12
7 8
8 9
9 0
In [55]:
1 data[['lightings', 'prices']]
Out[55]:
lightings prices
0 6 100
1 5 200
2 4 300
3 34 400
4 11 500
5 1 600
6 23 700
7 45 800
8 67 900
9 89 1000
localhost:8889/notebooks/Desktop/C4LEB/Desktop/python_class/Day_2_uni.ipynb 10/16
03/10/2023, 01:54 Day_2_uni - Jupyter Notebook
In [28]:
1 data
Out[28]:
0 1 6 high 100
1 2 5 low 200
2 3 4 mid 300
3 4 34 cid 400
4 5 11 kee 500
5 6 1 u 600
6 12 23 me 700
7 8 45 nine 800
8 9 67 ten 900
9 0 89 complet 1000
In [60]:
1 data.iloc[2:8,1:-1]
Out[60]:
lightings
2 4
3 34
4 11
5 1
6 23
7 45
In [30]:
1 y = data[['price']]
localhost:8889/notebooks/Desktop/C4LEB/Desktop/python_class/Day_2_uni.ipynb 11/16
03/10/2023, 01:54 Day_2_uni - Jupyter Notebook
In [31]:
1 x
Out[31]:
0 1 6 high
1 2 5 low
2 3 4 mid
3 4 34 cid
4 5 11 kee
5 6 1 u
6 12 23 me
7 8 45 nine
8 9 67 ten
9 0 89 complet
In [32]:
1 y
Out[32]:
price
0 100
1 200
2 300
3 400
4 500
5 600
6 700
7 800
8 900
9 1000
localhost:8889/notebooks/Desktop/C4LEB/Desktop/python_class/Day_2_uni.ipynb 12/16
03/10/2023, 01:54 Day_2_uni - Jupyter Notebook
In [33]:
1 data
Out[33]:
0 1 6 high 100
1 2 5 low 200
2 3 4 mid 300
3 4 34 cid 400
4 5 11 kee 500
5 6 1 u 600
6 12 23 me 700
7 8 45 nine 800
8 9 67 ten 900
9 0 89 complet 1000
In [34]:
localhost:8889/notebooks/Desktop/C4LEB/Desktop/python_class/Day_2_uni.ipynb 13/16
03/10/2023, 01:54 Day_2_uni - Jupyter Notebook
In [35]:
1 sns.distplot(data.price)
/home/C4LEB/anaconda3/lib/python3.9/site-packages/seaborn/distributi
ons.py:2619: FutureWarning: `distplot` is a deprecated function and
will be removed in a future version. Please adapt your code to use e
ither `displot` (a figure-level function with similar flexibility) o
r `histplot` (an axes-level function for histograms).
warnings.warn(msg, FutureWarning)
Out[35]:
<AxesSubplot:xlabel='price', ylabel='Density'>
localhost:8889/notebooks/Desktop/C4LEB/Desktop/python_class/Day_2_uni.ipynb 14/16
03/10/2023, 01:54 Day_2_uni - Jupyter Notebook
In [36]:
1 data.roofing.value_counts().plot(kind = 'bar');
In [37]:
1 data
Out[37]:
0 1 6 high 100
1 2 5 low 200
2 3 4 mid 300
3 4 34 cid 400
4 5 11 kee 500
5 6 1 u 600
6 12 23 me 700
7 8 45 nine 800
8 9 67 ten 900
9 0 89 complet 1000
localhost:8889/notebooks/Desktop/C4LEB/Desktop/python_class/Day_2_uni.ipynb 15/16
03/10/2023, 01:54 Day_2_uni - Jupyter Notebook
In [ ]:
In [39]:
1 sns.pairplot(data);
In [ ]:
localhost:8889/notebooks/Desktop/C4LEB/Desktop/python_class/Day_2_uni.ipynb 16/16