0% found this document useful (0 votes)
12 views

Session-25 - Jupyter Notebook

Uploaded by

patilyashyp22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Session-25 - Jupyter Notebook

Uploaded by

patilyashyp22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [1]: 1 import pandas as pd


2 import numpy as np

In [2]: 1 df=pd.read_csv('Iris.csv')
2 df

Out[2]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

0 1 5.1 3.5 1.4 0.2 Iris-setosa

1 2 4.9 3.0 1.4 0.2 Iris-setosa

2 3 4.7 3.2 1.3 0.2 Iris-setosa

3 4 4.6 3.1 1.5 0.2 Iris-setosa

4 5 5.0 3.6 1.4 0.2 Iris-setosa

... ... ... ... ... ... ...

145 146 6.7 3.0 5.2 2.3 Iris-virginica

146 147 6.3 2.5 5.0 1.9 Iris-virginica

147 148 6.5 3.0 5.2 2.0 Iris-virginica

148 149 6.2 3.4 5.4 2.3 Iris-virginica

149 150 5.9 3.0 5.1 1.8 Iris-virginica

150 rows × 6 columns

In [3]: 1 df.info()

<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

Acccess Data from DataFrame

1. Slicing :-

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 1/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [4]: 1 df=pd.read_csv('Iris.csv')
2 df

Out[4]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

0 1 5.1 3.5 1.4 0.2 Iris-setosa

1 2 4.9 3.0 1.4 0.2 Iris-setosa

2 3 4.7 3.2 1.3 0.2 Iris-setosa

3 4 4.6 3.1 1.5 0.2 Iris-setosa

4 5 5.0 3.6 1.4 0.2 Iris-setosa

... ... ... ... ... ... ...

145 146 6.7 3.0 5.2 2.3 Iris-virginica

146 147 6.3 2.5 5.0 1.9 Iris-virginica

147 148 6.5 3.0 5.2 2.0 Iris-virginica

148 149 6.2 3.4 5.4 2.3 Iris-virginica

149 150 5.9 3.0 5.1 1.8 Iris-virginica

150 rows × 6 columns

In [5]: 1 df[110:120]

Out[5]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

110 111 6.5 3.2 5.1 2.0 Iris-virginica

111 112 6.4 2.7 5.3 1.9 Iris-virginica

112 113 6.8 3.0 5.5 2.1 Iris-virginica

113 114 5.7 2.5 5.0 2.0 Iris-virginica

114 115 5.8 2.8 5.1 2.4 Iris-virginica

115 116 6.4 3.2 5.3 2.3 Iris-virginica

116 117 6.5 3.0 5.5 1.8 Iris-virginica

117 118 7.7 3.8 6.7 2.2 Iris-virginica

118 119 7.7 2.6 6.9 2.3 Iris-virginica

119 120 6.0 2.2 5.0 1.5 Iris-virginica

In [6]: 1 df[20:25] # Only for rows

Out[6]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

20 21 5.4 3.4 1.7 0.2 Iris-setosa

21 22 5.1 3.7 1.5 0.4 Iris-setosa

22 23 4.6 3.6 1.0 0.2 Iris-setosa

23 24 5.1 3.3 1.7 0.5 Iris-setosa

24 25 4.8 3.4 1.9 0.2 Iris-setosa

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 2/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [10]: 1 df[['SepalLengthCm','PetalLengthCm']]

Out[10]:
SepalLengthCm PetalLengthCm

0 5.1 1.4

1 4.9 1.4

2 4.7 1.3

3 4.6 1.5

4 5.0 1.4

... ... ...

145 6.7 5.2

146 6.3 5.0

147 6.5 5.2

148 6.2 5.4

149 5.9 5.1

150 rows × 2 columns

2.loc :-
In [ ]: 1 # 1. I is used to access the rows and columns by using name of rows and

In [12]: 1 df.loc[10:20,['SepalLengthCm','PetalLengthCm','Species']]

Out[12]:
SepalLengthCm PetalLengthCm Species

10 5.4 1.5 Iris-setosa

11 4.8 1.6 Iris-setosa

12 4.8 1.4 Iris-setosa

13 4.3 1.1 Iris-setosa

14 5.8 1.2 Iris-setosa

15 5.7 1.5 Iris-setosa

16 5.4 1.3 Iris-setosa

17 5.1 1.4 Iris-setosa

18 5.7 1.7 Iris-setosa

19 5.1 1.5 Iris-setosa

20 5.4 1.7 Iris-setosa

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 3/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [14]: 1 df.loc[110:120,'SepalLengthCm':'Species']

Out[14]:
SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

110 6.5 3.2 5.1 2.0 Iris-virginica

111 6.4 2.7 5.3 1.9 Iris-virginica

112 6.8 3.0 5.5 2.1 Iris-virginica

113 5.7 2.5 5.0 2.0 Iris-virginica

114 5.8 2.8 5.1 2.4 Iris-virginica

115 6.4 3.2 5.3 2.3 Iris-virginica

116 6.5 3.0 5.5 1.8 Iris-virginica

117 7.7 3.8 6.7 2.2 Iris-virginica

118 7.7 2.6 6.9 2.3 Iris-virginica

119 6.0 2.2 5.0 1.5 Iris-virginica

120 6.9 3.2 5.7 2.3 Iris-virginica

In [16]: 1 df.loc[110:120,['SepalLengthCm','Species']]

Out[16]:
SepalLengthCm Species

110 6.5 Iris-virginica

111 6.4 Iris-virginica

112 6.8 Iris-virginica

113 5.7 Iris-virginica

114 5.8 Iris-virginica

115 6.4 Iris-virginica

116 6.5 Iris-virginica

117 7.7 Iris-virginica

118 7.7 Iris-virginica

119 6.0 Iris-virginica

120 6.9 Iris-virginica

2.iloc:-
In [17]: 1 # 1. It is also used to access the rows and columns on the basis of inde
2 # last index no will be excluded

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 4/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [18]: 1 df=pd.read_csv('Iris.csv')
2 df

Out[18]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

0 1 5.1 3.5 1.4 0.2 Iris-setosa

1 2 4.9 3.0 1.4 0.2 Iris-setosa

2 3 4.7 3.2 1.3 0.2 Iris-setosa

3 4 4.6 3.1 1.5 0.2 Iris-setosa

4 5 5.0 3.6 1.4 0.2 Iris-setosa

... ... ... ... ... ... ...

145 146 6.7 3.0 5.2 2.3 Iris-virginica

146 147 6.3 2.5 5.0 1.9 Iris-virginica

147 148 6.5 3.0 5.2 2.0 Iris-virginica

148 149 6.2 3.4 5.4 2.3 Iris-virginica

149 150 5.9 3.0 5.1 1.8 Iris-virginica

150 rows × 6 columns

In [19]: 1 df.iloc[1,0]

Out[19]: 2

In [20]: 1 df.iloc[3,2] # df.iloc[rows,columns]

Out[20]: 3.1

In [21]: 1 df.iloc[2,5]

Out[21]: 'Iris-setosa'

In [22]: 1 df.iloc[2:5]

Out[22]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

2 3 4.7 3.2 1.3 0.2 Iris-setosa

3 4 4.6 3.1 1.5 0.2 Iris-setosa

4 5 5.0 3.6 1.4 0.2 Iris-setosa

In [23]: 1 df.iloc[2:5,1:3]

Out[23]:
SepalLengthCm SepalWidthCm

2 4.7 3.2

3 4.6 3.1

4 5.0 3.6

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 5/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [24]: 1 df1=df.groupby('SepalWidthCm').get_group(3.2)
2 df1

Out[24]:
Id SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species

2 3 4.7 3.2 1.3 0.2 Iris-setosa

29 30 4.7 3.2 1.6 0.2 Iris-setosa

35 36 5.0 3.2 1.2 0.2 Iris-setosa

42 43 4.4 3.2 1.3 0.2 Iris-setosa

47 48 4.6 3.2 1.4 0.2 Iris-setosa

50 51 7.0 3.2 4.7 1.4 Iris-versicolor

51 52 6.4 3.2 4.5 1.5 Iris-versicolor

70 71 5.9 3.2 4.8 1.8 Iris-versicolor

110 111 6.5 3.2 5.1 2.0 Iris-virginica

115 116 6.4 3.2 5.3 2.3 Iris-virginica

120 121 6.9 3.2 5.7 2.3 Iris-virginica

125 126 7.2 3.2 6.0 1.8 Iris-virginica

143 144 6.8 3.2 5.9 2.3 Iris-virginica

Delete columns / rows

1. Delete columns :-
In [13]: 1 import pandas as pd
2 import numpy as np
3 ​
4 data={'Name':['A','B','C','D'],
5 'Last_name':['E','F','G','H']}
6 df=pd.DataFrame(data)
7 df

Out[13]:
Name Last_name

0 A E

1 B F

2 C G

3 D H

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 6/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [14]: 1 df['score']=[10,20,30,40]
2 df['Zeros']=np.zeros(4)
3 df['ones']=np.ones(4)
4 df

Out[14]:
Name Last_name score Zeros ones

0 A E 10 0.0 1.0

1 B F 20 0.0 1.0

2 C G 30 0.0 1.0

3 D H 40 0.0 1.0

1 aixs=0 >>> rows


2 axis=1 >>> columns

In [15]: 1 df=df.drop(['Zeros'],axis=1)
2 df

Out[15]:
Name Last_name score ones

0 A E 10 1.0

1 B F 20 1.0

2 C G 30 1.0

3 D H 40 1.0

In [17]: 1 df=df.drop(['score','ones'],axis=1)
2 df

Out[17]:
Name Last_name

0 A E

1 B F

2 C G

3 D H

In [21]: 1 df1=df.T
2 df1

Out[21]:
0 1 2 3

Name A B C D

Last_name E F G H

In [22]: 1 df1=df1.drop('Last_name',axis=0)
2 df1

Out[22]:
0 1 2 3

Name A B C D

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 7/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [23]: 1 import pandas as pd


2 import numpy as np
3 ​
4 data={'Name':['A','B','C','D'],
5 'Last_name':['E','F','G','H']}
6 df=pd.DataFrame(data)
7 df

Out[23]:
Name Last_name

0 A E

1 B F

2 C G

3 D H

In [24]: 1 df['score']=[10,20,30,40]
2 df['Zeros']=np.zeros(4)
3 df['ones']=np.ones(4)
4 df

Out[24]:
Name Last_name score Zeros ones

0 A E 10 0.0 1.0

1 B F 20 0.0 1.0

2 C G 30 0.0 1.0

3 D H 40 0.0 1.0

loc:-
In [25]: 1 df.loc[1:2]

Out[25]:
Name Last_name score Zeros ones

1 B F 20 0.0 1.0

2 C G 30 0.0 1.0

In [27]: 1 df.loc[1:3,['score','Zeros']]

Out[27]:
score Zeros

1 20 0.0

2 30 0.0

3 40 0.0

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 8/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [29]: 1 df.loc[1:3,'score':'ones']

Out[29]:
score Zeros ones

1 20 0.0 1.0

2 30 0.0 1.0

3 40 0.0 1.0

In [30]: 1 # iloc

In [31]: 1 df

Out[31]:
Name Last_name score Zeros ones

0 A E 10 0.0 1.0

1 B F 20 0.0 1.0

2 C G 30 0.0 1.0

3 D H 40 0.0 1.0

In [32]: 1 df.iloc[0:2]

Out[32]:
Name Last_name score Zeros ones

0 A E 10 0.0 1.0

1 B F 20 0.0 1.0

In [33]: 1 df.iloc[:,:]

Out[33]:
Name Last_name score Zeros ones

0 A E 10 0.0 1.0

1 B F 20 0.0 1.0

2 C G 30 0.0 1.0

3 D H 40 0.0 1.0

In [34]: 1 df.iloc[2,2]

Out[34]: 30

In [35]: 1 df.iloc[:,1:3]

Out[35]:
Last_name score

0 E 10

1 F 20

2 G 30

3 H 40

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 9/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook
1 iloc >> index
2 loc >> Name of rows and columns

1 1. head()
2 2. tail()
3 3.info()
4 4.shape() >> tuple of rows and columns
5 5.describe() >> statistical clculation
6 6.Index() >> rows
7 7.columns() >> columns name
8 8.T >>> transpose
9 9. loc
10 10iloc
11 11.drop

In [36]: 1 df=pd.read_excel('Emp_Records.xlsx')
2 df

Out[36]:
Emp First Age in Weight in Age in Unnamed:
City Salary
ID Name Yrs Kgs Company 5

0 677509 Lois 36.36 60 13.68 NaN Denver 168251

1 940761 Brenda 47.02 60 9.01 NaN Stonewall 51063

2 428945 Joe 54.15 68 0.98 NaN Michigantown 50155

3 408351 Diane 39.67 51 18.30 NaN Hydetown 180294

4 193819 Benjamin 40.31 58 4.01 NaN Fremont 117642

... ... ... ... ... ... ... ... ...

95 639892 Jose 22.82 89 1.05 NaN Biloxi 129774

96 704709 Harold 32.61 77 5.93 NaN Carol Stream 156194

97 461593 Nicole 52.66 60 28.53 NaN Detroit 95673

98 392491 Theresa 29.60 57 6.99 NaN Mc Grath 51015

99 495141 Tammy 38.38 55 2.26 NaN Alma 93650

100 rows × 8 columns

In [37]: 1 df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 100 entries, 0 to 99
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Emp ID 100 non-null int64
1 First Name 100 non-null object
2 Age in Yrs 100 non-null float64
3 Weight in Kgs 100 non-null int64
4 Age in Company 100 non-null float64
5 Unnamed: 5 0 non-null float64
6 City 100 non-null object
7 Salary 100 non-null int64
dtypes: float64(3), int64(3), object(2)
memory usage: 6.4+ KB

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 10/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [38]: 1 df.dtypes

Out[38]: Emp ID int64


First Name object
Age in Yrs float64
Weight in Kgs int64
Age in Company float64
Unnamed: 5 float64
City object
Salary int64
dtype: object

In [39]: 1 df.columns

Out[39]: Index(['Emp ID', 'First Name', 'Age in Yrs', 'Weight in Kgs', 'Age in Compa
ny',
'Unnamed: 5', 'City', 'Salary'],
dtype='object')

In [40]: 1 df.axes

Out[40]: [RangeIndex(start=0, stop=100, step=1),


Index(['Emp ID', 'First Name', 'Age in Yrs', 'Weight in Kgs', 'Age in Comp
any',
'Unnamed: 5', 'City', 'Salary'],
dtype='object')]

In [41]: 1 df.describe()

Out[41]:
Weight in Age in Unnamed:
Emp ID Age in Yrs Salary
Kgs Company 5

count 100.00000 100.000000 100.000000 100.000000 0.0 100.000000

mean 547652.10000 39.238700 58.080000 8.978400 NaN 119738.090000

std 257664.16679 12.066252 12.294106 8.657358 NaN 46185.278194

min 134841.00000 21.100000 40.000000 0.020000 NaN 42005.000000

25% 328643.75000 28.177500 50.000000 2.152500 NaN 83979.750000

50% 497414.00000 37.595000 56.000000 6.435000 NaN 118049.500000

75% 766040.00000 49.900000 61.250000 13.762500 NaN 162509.250000

max 979607.00000 59.470000 90.000000 34.520000 NaN 197537.000000

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 11/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [42]: 1 df

Out[42]:
Emp First Age in Weight in Age in Unnamed:
City Salary
ID Name Yrs Kgs Company 5

0 677509 Lois 36.36 60 13.68 NaN Denver 168251

1 940761 Brenda 47.02 60 9.01 NaN Stonewall 51063

2 428945 Joe 54.15 68 0.98 NaN Michigantown 50155

3 408351 Diane 39.67 51 18.30 NaN Hydetown 180294

4 193819 Benjamin 40.31 58 4.01 NaN Fremont 117642

... ... ... ... ... ... ... ... ...

95 639892 Jose 22.82 89 1.05 NaN Biloxi 129774

96 704709 Harold 32.61 77 5.93 NaN Carol Stream 156194

97 461593 Nicole 52.66 60 28.53 NaN Detroit 95673

98 392491 Theresa 29.60 57 6.99 NaN Mc Grath 51015

99 495141 Tammy 38.38 55 2.26 NaN Alma 93650

100 rows × 8 columns

use of index_col
In [43]: 1 df=pd.read_excel('Emp_Records.xlsx',index_col='Age in Company')
2 df

Out[43]:
Emp First Age in Weight in Unnamed:
City Salary
ID Name Yrs Kgs 5

Age in
Company

13.68 677509 Lois 36.36 60 NaN Denver 168251

9.01 940761 Brenda 47.02 60 NaN Stonewall 51063

0.98 428945 Joe 54.15 68 NaN Michigantown 50155

18.30 408351 Diane 39.67 51 NaN Hydetown 180294

4.01 193819 Benjamin 40.31 58 NaN Fremont 117642

... ... ... ... ... ... ... ...

1.05 639892 Jose 22.82 89 NaN Biloxi 129774

5.93 704709 Harold 32.61 77 NaN Carol Stream 156194

28.53 461593 Nicole 52.66 60 NaN Detroit 95673

6.99 392491 Theresa 29.60 57 NaN Mc Grath 51015

2.26 495141 Tammy 38.38 55 NaN Alma 93650

100 rows × 7 columns

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 12/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [44]: 1 df=pd.read_excel('Emp_Records.xlsx',index_col='First Name')


2 df

Out[44]:
Emp Age in Weight in Age in Unnamed:
City Salary
ID Yrs Kgs Company 5

First
Name

Lois 677509 36.36 60 13.68 NaN Denver 168251

Brenda 940761 47.02 60 9.01 NaN Stonewall 51063

Joe 428945 54.15 68 0.98 NaN Michigantown 50155

Diane 408351 39.67 51 18.30 NaN Hydetown 180294

Benjamin 193819 40.31 58 4.01 NaN Fremont 117642

... ... ... ... ... ... ... ...

Jose 639892 22.82 89 1.05 NaN Biloxi 129774

Harold 704709 32.61 77 5.93 NaN Carol Stream 156194

Nicole 461593 52.66 60 28.53 NaN Detroit 95673

Theresa 392491 29.60 57 6.99 NaN Mc Grath 51015

Tammy 495141 38.38 55 2.26 NaN Alma 93650

100 rows × 7 columns

In [45]: 1 df

Out[45]:
Emp Age in Weight in Age in Unnamed:
City Salary
ID Yrs Kgs Company 5

First
Name

Lois 677509 36.36 60 13.68 NaN Denver 168251

Brenda 940761 47.02 60 9.01 NaN Stonewall 51063

Joe 428945 54.15 68 0.98 NaN Michigantown 50155

Diane 408351 39.67 51 18.30 NaN Hydetown 180294

Benjamin 193819 40.31 58 4.01 NaN Fremont 117642

... ... ... ... ... ... ... ...

Jose 639892 22.82 89 1.05 NaN Biloxi 129774

Harold 704709 32.61 77 5.93 NaN Carol Stream 156194

Nicole 461593 52.66 60 28.53 NaN Detroit 95673

Theresa 392491 29.60 57 6.99 NaN Mc Grath 51015

Tammy 495141 38.38 55 2.26 NaN Alma 93650

100 rows × 7 columns

create csv file

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 13/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [46]: 1 var1={'a':[100,200,300,400],
2 'b':[10,20,30,40]}
3 df=pd.DataFrame(var1)
4 df

Out[46]:
a b

0 100 10

1 200 20

2 300 30

3 400 40

In [47]: 1 df.to_csv('techie.csv')

In [48]: 1 df.to_excel('techie1.xlsx')

sorting :-
In [49]: 1 df=pd.read_excel('Emp_Records.xlsx')
2 df

Out[49]:
Emp First Age in Weight in Age in Unnamed:
City Salary
ID Name Yrs Kgs Company 5

0 677509 Lois 36.36 60 13.68 NaN Denver 168251

1 940761 Brenda 47.02 60 9.01 NaN Stonewall 51063

2 428945 Joe 54.15 68 0.98 NaN Michigantown 50155

3 408351 Diane 39.67 51 18.30 NaN Hydetown 180294

4 193819 Benjamin 40.31 58 4.01 NaN Fremont 117642

... ... ... ... ... ... ... ... ...

95 639892 Jose 22.82 89 1.05 NaN Biloxi 129774

96 704709 Harold 32.61 77 5.93 NaN Carol Stream 156194

97 461593 Nicole 52.66 60 28.53 NaN Detroit 95673

98 392491 Theresa 29.60 57 6.99 NaN Mc Grath 51015

99 495141 Tammy 38.38 55 2.26 NaN Alma 93650

100 rows × 8 columns

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 14/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [51]: 1 df.sort_index(axis=1)

Out[51]:
Age in Age in Emp First Unnamed: Weight in
City Salary
Company Yrs ID Name 5 Kgs

0 13.68 36.36 Denver 677509 Lois 168251 NaN 60

1 9.01 47.02 Stonewall 940761 Brenda 51063 NaN 60

2 0.98 54.15 Michigantown 428945 Joe 50155 NaN 68

3 18.30 39.67 Hydetown 408351 Diane 180294 NaN 51

4 4.01 40.31 Fremont 193819 Benjamin 117642 NaN 58

... ... ... ... ... ... ... ... ...

95 1.05 22.82 Biloxi 639892 Jose 129774 NaN 89

96 5.93 32.61 Carol Stream 704709 Harold 156194 NaN 77

97 28.53 52.66 Detroit 461593 Nicole 95673 NaN 60

98 6.99 29.60 Mc Grath 392491 Theresa 51015 NaN 57

99 2.26 38.38 Alma 495141 Tammy 93650 NaN 55

100 rows × 8 columns

In [52]: 1 df1=pd.read_excel('Emp_Records.xlsx',index_col='City')
2 df1

Out[52]:
Emp First Age in Weight in Age in Unnamed:
Salary
ID Name Yrs Kgs Company 5

City

Denver 677509 Lois 36.36 60 13.68 NaN 168251

Stonewall 940761 Brenda 47.02 60 9.01 NaN 51063

Michigantown 428945 Joe 54.15 68 0.98 NaN 50155

Hydetown 408351 Diane 39.67 51 18.30 NaN 180294

Fremont 193819 Benjamin 40.31 58 4.01 NaN 117642

... ... ... ... ... ... ... ...

Biloxi 639892 Jose 22.82 89 1.05 NaN 129774

Carol Stream 704709 Harold 32.61 77 5.93 NaN 156194

Detroit 461593 Nicole 52.66 60 28.53 NaN 95673

Mc Grath 392491 Theresa 29.60 57 6.99 NaN 51015

Alma 495141 Tammy 38.38 55 2.26 NaN 93650

100 rows × 7 columns

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 15/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [53]: 1 df1.sort_index(axis=0)

Out[53]:
Emp Age in Weight in Age in Unnamed:
First Name Salary
ID Yrs Kgs Company 5

City

Albion 765850 Linda 25.96 40 0.20 NaN 113256

Alcoa 316110 Jeremy 52.51 52 11.33 NaN 178847

Alma 495141 Tammy 38.38 55 2.26 NaN 93650

Arlee 904898 Ann 24.61 44 0.45 NaN 182521

Atlanta 539712 Nancy 22.14 50 0.87 NaN 98189

... ... ... ... ... ... ... ...

Whiteman Air
969964 Janice 37.57 56 0.93 NaN 147641
Force Base

Wichita 476433 Lillian 42.79 55 17.65 NaN 149878

Willow Beach 917395 Christopher 57.37 62 19.73 NaN 190765

Woodbury 388642 Ruby 37.27 59 3.91 NaN 160623

Wright 726264 Carl 43.63 90 10.14 NaN 162159

100 rows × 7 columns

In [54]: 1 df.sort_index(axis=1,ascending=False)

Out[54]:
Weight in Unnamed: First Emp Age in Age in
Salary City
Kgs 5 Name ID Yrs Company

0 60 NaN 168251 Lois 677509 Denver 36.36 13.68

1 60 NaN 51063 Brenda 940761 Stonewall 47.02 9.01

2 68 NaN 50155 Joe 428945 Michigantown 54.15 0.98

3 51 NaN 180294 Diane 408351 Hydetown 39.67 18.30

4 58 NaN 117642 Benjamin 193819 Fremont 40.31 4.01

... ... ... ... ... ... ... ... ...

95 89 NaN 129774 Jose 639892 Biloxi 22.82 1.05

96 77 NaN 156194 Harold 704709 Carol Stream 32.61 5.93

97 60 NaN 95673 Nicole 461593 Detroit 52.66 28.53

98 57 NaN 51015 Theresa 392491 Mc Grath 29.60 6.99

99 55 NaN 93650 Tammy 495141 Alma 38.38 2.26

100 rows × 8 columns

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 16/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [55]: 1 df1.sort_index(axis=0,ascending=False)

Out[55]:
Emp Age in Weight in Age in Unnamed:
First Name Salary
ID Yrs Kgs Company 5

City

Wright 726264 Carl 43.63 90 10.14 NaN 162159

Woodbury 388642 Ruby 37.27 59 3.91 NaN 160623

Willow Beach 917395 Christopher 57.37 62 19.73 NaN 190765

Wichita 476433 Lillian 42.79 55 17.65 NaN 149878

Whiteman Air
969964 Janice 37.57 56 0.93 NaN 147641
Force Base

... ... ... ... ... ... ... ...

Atlanta 539712 Nancy 22.14 50 0.87 NaN 98189

Arlee 904898 Ann 24.61 44 0.45 NaN 182521

Alma 495141 Tammy 38.38 55 2.26 NaN 93650

Alcoa 316110 Jeremy 52.51 52 11.33 NaN 178847

Albion 765850 Linda 25.96 40 0.20 NaN 113256

100 rows × 7 columns

In [56]: 1 df1=pd.read_excel('Emp_Records.xlsx',index_col='City')
2 df1

Out[56]:
Emp First Age in Weight in Age in Unnamed:
Salary
ID Name Yrs Kgs Company 5

City

Denver 677509 Lois 36.36 60 13.68 NaN 168251

Stonewall 940761 Brenda 47.02 60 9.01 NaN 51063

Michigantown 428945 Joe 54.15 68 0.98 NaN 50155

Hydetown 408351 Diane 39.67 51 18.30 NaN 180294

Fremont 193819 Benjamin 40.31 58 4.01 NaN 117642

... ... ... ... ... ... ... ...

Biloxi 639892 Jose 22.82 89 1.05 NaN 129774

Carol Stream 704709 Harold 32.61 77 5.93 NaN 156194

Detroit 461593 Nicole 52.66 60 28.53 NaN 95673

Mc Grath 392491 Theresa 29.60 57 6.99 NaN 51015

Alma 495141 Tammy 38.38 55 2.26 NaN 93650

100 rows × 7 columns

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 17/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [59]: 1 df1.reset_index()
2 ​

Out[59]:
Emp First Age in Weight in Age in Unnamed:
City Salary
ID Name Yrs Kgs Company 5

0 Denver 677509 Lois 36.36 60 13.68 NaN 168251

1 Stonewall 940761 Brenda 47.02 60 9.01 NaN 51063

2 Michigantown 428945 Joe 54.15 68 0.98 NaN 50155

3 Hydetown 408351 Diane 39.67 51 18.30 NaN 180294

4 Fremont 193819 Benjamin 40.31 58 4.01 NaN 117642

... ... ... ... ... ... ... ... ...

95 Biloxi 639892 Jose 22.82 89 1.05 NaN 129774

96 Carol Stream 704709 Harold 32.61 77 5.93 NaN 156194

97 Detroit 461593 Nicole 52.66 60 28.53 NaN 95673

98 Mc Grath 392491 Theresa 29.60 57 6.99 NaN 51015

99 Alma 495141 Tammy 38.38 55 2.26 NaN 93650

In [60]: 1 df1=pd.read_excel('Emp_Records.xlsx',index_col='City')
2 df1

Out[60]:
Emp First Age in Weight in Age in Unnamed:
Salary
ID Name Yrs Kgs Company 5

City

Denver 677509 Lois 36.36 60 13.68 NaN 168251

Stonewall 940761 Brenda 47.02 60 9.01 NaN 51063

Michigantown 428945 Joe 54.15 68 0.98 NaN 50155

Hydetown 408351 Diane 39.67 51 18.30 NaN 180294

Fremont 193819 Benjamin 40.31 58 4.01 NaN 117642

... ... ... ... ... ... ... ...

Biloxi 639892 Jose 22.82 89 1.05 NaN 129774

Carol Stream 704709 Harold 32.61 77 5.93 NaN 156194

Detroit 461593 Nicole 52.66 60 28.53 NaN 95673

Mc Grath 392491 Theresa 29.60 57 6.99 NaN 51015

Alma 495141 Tammy 38.38 55 2.26 NaN 93650

100 rows × 7 columns

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 18/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [61]: 1 df1.reset_index(drop=True)
2 # df.rest_index(drop=False) >> i dont have to drop
Emp ID First Name Age in Yrs Weight in Kgs Age in Company Unnamed: 5 Salary

0 677509 Lois 36.36 60 13.68 NaN 168251

1 940761 Brenda 47.02 60 9.01 NaN 51063

2 428945 Joe 54.15 68 0.98 NaN 50155

3 408351 Diane 39.67 51 18.30 NaN 180294

4 193819 Benjamin 40.31 58 4.01 NaN 117642

... ... ... ... ... ... ... ...

95 639892 Jose 22.82 89 1.05 NaN 129774

96 704709 Harold 32.61 77 5.93 NaN 156194

97 461593 Nicole 52.66 60 28.53 NaN 95673

98 392491 Theresa 29.60 57 6.99 NaN 51015

99 495141 Tammy 38.38 55 2.26 NaN 93650

100 rows × 7 columns

sort values () :-
In [62]: 1 df

Out[62]:
Emp First Age in Weight in Age in Unnamed:
City Salary
ID Name Yrs Kgs Company 5

0 677509 Lois 36.36 60 13.68 NaN Denver 168251

1 940761 Brenda 47.02 60 9.01 NaN Stonewall 51063

2 428945 Joe 54.15 68 0.98 NaN Michigantown 50155

3 408351 Diane 39.67 51 18.30 NaN Hydetown 180294

4 193819 Benjamin 40.31 58 4.01 NaN Fremont 117642

... ... ... ... ... ... ... ... ...

95 639892 Jose 22.82 89 1.05 NaN Biloxi 129774

96 704709 Harold 32.61 77 5.93 NaN Carol Stream 156194

97 461593 Nicole 52.66 60 28.53 NaN Detroit 95673

98 392491 Theresa 29.60 57 6.99 NaN Mc Grath 51015

99 495141 Tammy 38.38 55 2.26 NaN Alma 93650

100 rows × 8 columns

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 19/20
3/15/24, 9:42 PM Session-25 - Jupyter Notebook

In [63]: 1 df.sort_values(['Age in Yrs'])

Out[63]:
Emp First Age in Weight in Age in Unnamed:
City Salary
ID Name Yrs Kgs Company 5

13 301576 Wayne 21.10 87 0.02 NaN Maida 92758

49 879753 Pamela 21.30 47 0.13 NaN Banner 149262

59 750173 Antonio 21.93 82 0.24 NaN Mc Calla 181646

6 539712 Nancy 22.14 50 0.87 NaN Atlanta 98189

11 153989 Jack 22.21 61 0.56 NaN Las Vegas 82965

... ... ... ... ... ... ... ... ...

74 528673 Paul 58.43 60 22.10 NaN Blue River 145235

7 380086 Carol 59.12 40 34.52 NaN Blanchester 60918

57 515103 Anne 59.27 48 14.01 NaN Cookeville 114426

24 560455 Carolyn 59.42 53 16.08 NaN Saint Cloud 42005

14 441771 Cheryl 59.47 47 26.69 NaN Quecreek 92220

100 rows × 8 columns

In [64]: 1 df.sort_values(['Weight in Kgs'])

Out[64]:
Emp First Age in Weight in Age in Unnamed:
City Salary
ID Name Yrs Kgs Company 5

Lake
41 227922 Amanda 35.02 40 10.28 NaN 114257
Charles

7 380086 Carol 59.12 40 34.52 NaN Blanchester 60918

75 765850 Linda 25.96 40 0.20 NaN Albion 113256

47 524896 Judy 56.38 40 5.59 NaN Topeka 133332

51 447813 Ann 28.23 41 3.69 NaN Hancock 130014

... ... ... ... ... ... ... ... ...

87 623929 Jimmy 50.70 87 9.63 NaN Oriskany 120631

13 301576 Wayne 21.10 87 0.02 NaN Maida 92758

Saranac
82 761821 Ernest 32.77 87 2.49 NaN 176675
Lake

95 639892 Jose 22.82 89 1.05 NaN Biloxi 129774

38 726264 Carl 43.63 90 10.14 NaN Wright 162159

100 rows × 8 columns

In [ ]: 1 ​

localhost:8888/notebooks/Desktop/Techpaathsala/Session-25.ipynb 20/20

You might also like