Download as pdf or txt
Download as pdf or txt
You are on page 1of 69

9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Week 01
Thursday 26 August 2021

What we have done till now


Data types
Conditionals and Looping
User Defined Functions
Pandas (basics)

Agenda for today


Pandas

Your Excel Sheet in Python

You are going to spend a lot of time with Pandas.

Master Pandas

https://pandas.pydata.org/docs/

In [1]: import pandas as pd # Alias

Read a File

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 1/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

image Source:

Data Source:

CSV file

read_csv

https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html

In [2]: pokemon_df = pd.read_csv(r'C:\Vivek\Misc\Simplilearn\Pokemon.csv')

pokemon_df

Out[2]: Type Sp. Sp.


Name Type 1 Total HP Attack Defense Speed Generation Legenda
2 Atk Def

0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 Fa

1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 Fa

2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 Fa

VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur

4 Charmander Fire NaN 309 39 52 43 60 50 65 1 Fa

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

795 Diancie Rock Fairy 600 50 100 150 100 150 50 6 T

DiancieMega
796 Rock Fairy 700 50 160 110 160 110 110 6 T
Diancie

HoopaHoopa
797 Psychic Ghost 600 80 110 60 150 130 70 6 T
Confined

HoopaHoopa
798 Psychic Dark 680 80 160 60 170 130 80 6 T
Unbound

799 Volcanion Fire Water 600 80 110 120 130 90 70 6 T

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 2/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

800 rows × 12 columns

In [ ]:

CSV file without headers

In [3]: pokemon_v2_df = pd.read_csv(r'C:\Vivek\Misc\Simplilearn\Pokemon v2.csv', header= None)

pokemon_v2_df

Out[3]: 0 1 2 3 4 5 6 7 8 9 10 11

0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 False

1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 False

2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 False

3 VenusaurMega Venusaur Grass Poison 625 80 100 123 122 120 80 1 False

4 Charmander Fire NaN 309 39 52 43 60 50 65 1 False

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

795 Diancie Rock Fairy 600 50 100 150 100 150 50 6 True

796 DiancieMega Diancie Rock Fairy 700 50 160 110 160 110 110 6 True

797 HoopaHoopa Confined Psychic Ghost 600 80 110 60 150 130 70 6 True

798 HoopaHoopa Unbound Psychic Dark 680 80 160 60 170 130 80 6 True

799 Volcanion Fire Water 600 80 110 120 130 90 70 6 True

800 rows × 12 columns

Read only the first n rows - use the nrows arguement

In [4]: pokemon_v2_df = pd.read_csv(r'C:\Vivek\Misc\Simplilearn\Pokemon v2.csv', header = None,


pokemon_v2_df

Out[4]: 0 1 2 3 4 5 6 7 8 9 10 11

0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 False

1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 False

2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 False

3 VenusaurMega Venusaur Grass Poison 625 80 100 123 122 120 80 1 False

4 Charmander Fire NaN 309 39 52 43 60 50 65 1 False

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

95 Grimer Poison NaN 325 80 80 50 40 50 25 1 False

96 Muk Poison NaN 500 105 105 75 65 100 50 1 False

97 Shellder Water NaN 305 30 65 100 45 25 40 1 False

98 Cloyster Water Ice 525 50 95 180 85 45 70 1 False

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 3/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

0 1 2 3 4 5 6 7 8 9 10 11

99 Gastly Ghost Poison 310 30 35 30 100 35 80 1 False

100 rows × 12 columns

Tab Separated file

In [5]: pokemon_txt_df = pd.read_csv('Pokemon.txt', delimiter = '\t')

pokemon_txt_df

Out[5]: Type Sp. Sp.


Name Type 1 Total HP Attack Defense Speed Generation Legenda
2 Atk Def

0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 Fa

1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 Fa

2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 Fa

VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur

4 Charmander Fire NaN 309 39 52 43 60 50 65 1 Fa

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

795 Diancie Rock Fairy 600 50 100 150 100 150 50 6 T

DiancieMega
796 Rock Fairy 700 50 160 110 160 110 110 6 T
Diancie

HoopaHoopa
797 Psychic Ghost 600 80 110 60 150 130 70 6 T
Confined

HoopaHoopa
798 Psychic Dark 680 80 160 60 170 130 80 6 T
Unbound

799 Volcanion Fire Water 600 80 110 120 130 90 70 6 T

800 rows × 12 columns

Excel File

read_excel

https://pandas.pydata.org/docs/reference/api/pandas.read_excel.html

In [6]: pokemon_excel_df = pd.read_excel('Pokemon.xlsx')

pokemon_excel_df

Out[6]: Type Sp. Sp.


Name Type 1 Total HP Attack Defense Speed Generation Legenda
2 Atk Def

0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 Fa

1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 Fa

2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 Fa

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 4/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Type Sp. Sp.


Name Type 1 Total HP Attack Defense Speed Generation Legenda
2 Atk Def

VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur

4 Charmander Fire NaN 309 39 52 43 60 50 65 1 Fa

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

795 Diancie Rock Fairy 600 50 100 150 100 150 50 6 T

DiancieMega
796 Rock Fairy 700 50 160 110 160 110 110 6 T
Diancie

HoopaHoopa
797 Psychic Ghost 600 80 110 60 150 130 70 6 T
Confined

HoopaHoopa
798 Psychic Dark 680 80 160 60 170 130 80 6 T
Unbound

799 Volcanion Fire Water 600 80 110 120 130 90 70 6 T

800 rows × 12 columns

Basic Operations
Exploring the Data

In [7]: pokemon_df = pd.read_csv(r'C:\Vivek\Misc\Simplilearn\Pokemon.csv')

pokemon_df

Out[7]: Type Sp. Sp.


Name Type 1 Total HP Attack Defense Speed Generation Legenda
2 Atk Def

0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 Fa

1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 Fa

2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 Fa

VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur

4 Charmander Fire NaN 309 39 52 43 60 50 65 1 Fa

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

795 Diancie Rock Fairy 600 50 100 150 100 150 50 6 T

DiancieMega
796 Rock Fairy 700 50 160 110 160 110 110 6 T
Diancie

HoopaHoopa
797 Psychic Ghost 600 80 110 60 150 130 70 6 T
Confined

HoopaHoopa
798 Psychic Dark 680 80 160 60 170 130 80 6 T
Unbound

799 Volcanion Fire Water 600 80 110 120 130 90 70 6 T

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 5/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

800 rows × 12 columns

info

In [8]: pokemon_df.info()

<class 'pandas.core.frame.DataFrame'>

RangeIndex: 800 entries, 0 to 799

Data columns (total 12 columns):

# Column Non-Null Count Dtype

--- ------ -------------- -----

0 Name 800 non-null object

1 Type 1 800 non-null object

2 Type 2 414 non-null object

3 Total 800 non-null int64

4 HP 800 non-null int64

5 Attack 800 non-null int64

6 Defense 800 non-null int64

7 Sp. Atk 800 non-null int64

8 Sp. Def 800 non-null int64

9 Speed 800 non-null int64

10 Generation 800 non-null int64

11 Legendary 800 non-null bool

dtypes: bool(1), int64(8), object(3)

memory usage: 69.7+ KB

shape

In [9]: pokemon_df.shape

Out[9]: (800, 12)

describe

In [10]: pokemon_df.describe()

Out[10]: Total HP Attack Defense Sp. Atk Sp. Def Speed Generation

count 800.00000 800.000000 800.000000 800.000000 800.000000 800.000000 800.000000 800.00000

mean 435.10250 69.258750 79.001250 73.842500 72.820000 71.902500 68.277500 3.32375

std 119.96304 25.534669 32.457366 31.183501 32.722294 27.828916 29.060474 1.66129

min 180.00000 1.000000 5.000000 5.000000 10.000000 20.000000 5.000000 1.00000

25% 330.00000 50.000000 55.000000 50.000000 49.750000 50.000000 45.000000 2.00000

50% 450.00000 65.000000 75.000000 70.000000 65.000000 70.000000 65.000000 3.00000

75% 515.00000 80.000000 100.000000 90.000000 95.000000 90.000000 90.000000 5.00000

max 780.00000 255.000000 190.000000 230.000000 194.000000 230.000000 180.000000 6.00000

In [11]: pokemon_df.describe(include = 'all') # For all types of data

Out[11]: Type Type


Name Total HP Attack Defense Sp. Atk Sp. Def
1 2

count 800 800 414 800.00000 800.000000 800.000000 800.000000 800.000000 800.000000

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 6/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Type Type
Name Total HP Attack Defense Sp. Atk Sp. Def
1 2

unique 800 18 18 NaN NaN NaN NaN NaN NaN

top Mandibuzz Water Flying NaN NaN NaN NaN NaN NaN

freq 1 112 97 NaN NaN NaN NaN NaN NaN

mean NaN NaN NaN 435.10250 69.258750 79.001250 73.842500 72.820000 71.902500

std NaN NaN NaN 119.96304 25.534669 32.457366 31.183501 32.722294 27.828916

min NaN NaN NaN 180.00000 1.000000 5.000000 5.000000 10.000000 20.000000

25% NaN NaN NaN 330.00000 50.000000 55.000000 50.000000 49.750000 50.000000

50% NaN NaN NaN 450.00000 65.000000 75.000000 70.000000 65.000000 70.000000

75% NaN NaN NaN 515.00000 80.000000 100.000000 90.000000 95.000000 90.000000

max NaN NaN NaN 780.00000 255.000000 190.000000 230.000000 194.000000 230.000000

Head

In [12]: pokemon_df.head()

Out[12]: Type Type Sp. Sp.


Name Total HP Attack Defense Speed Generation Legendary
1 2 Atk Def

0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 False

1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 False

2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 False

VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 False
Venusaur

4 Charmander Fire NaN 309 39 52 43 60 50 65 1 False

In [13]: pokemon_df.head(20) # First N default is 5

Out[13]: Type Sp. Sp.


Name Type 2 Total HP Attack Defense Speed Generation Legendar
1 Atk Def

0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 Fals

1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 Fals

2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 Fals

VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fals
Venusaur

4 Charmander Fire NaN 309 39 52 43 60 50 65 1 Fals

5 Charmeleon Fire NaN 405 58 64 58 80 65 80 1 Fals

6 Charizard Fire Flying 534 78 84 78 109 85 100 1 Fals

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 7/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Type Sp. Sp.


Name Type 2 Total HP Attack Defense Speed Generation Legendar
1 Atk Def

CharizardMega
7 Fire Dragon 634 78 130 111 130 85 100 1 Fals
Charizard X

CharizardMega
8 Fire Flying 634 78 104 78 159 115 100 1 Fals
Charizard Y

9 Squirtle Water NaN 314 44 48 65 50 64 43 1 Fals

10 Wartortle Water NaN 405 59 63 80 65 80 58 1 Fals

11 Blastoise Water NaN 530 79 83 100 85 105 78 1 Fals

BlastoiseMega
12 Water NaN 630 79 103 120 135 115 78 1 Fals
Blastoise

13 Caterpie Bug NaN 195 45 30 35 20 20 45 1 Fals

14 Metapod Bug NaN 205 50 20 55 25 25 30 1 Fals

15 Butterfree Bug Flying 395 60 45 50 90 80 70 1 Fals

16 Weedle Bug Poison 195 40 35 30 20 20 50 1 Fals

17 Kakuna Bug Poison 205 45 25 50 25 25 35 1 Fals

18 Beedrill Bug Poison 395 65 90 40 45 80 75 1 Fals

BeedrillMega
19 Bug Poison 495 65 150 40 15 80 145 1 Fals
Beedrill

Tail

In [14]: pokemon_df.tail(10) # bottom N - default = 5

Out[14]: Sp. Sp.


Name Type 1 Type 2 Total HP Attack Defense Speed Generation Legend
Atk Def

790 Noibat Flying Dragon 245 40 30 35 45 40 55 6 Fa

791 Noivern Flying Dragon 535 85 70 80 97 80 123 6 Fa

792 Xerneas Fairy NaN 680 126 131 95 131 98 99 6 T

793 Yveltal Dark Flying 680 126 131 95 131 98 99 6 T

Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6 T
Forme

795 Diancie Rock Fairy 600 50 100 150 100 150 50 6 T

DiancieMega
796 Rock Fairy 700 50 160 110 160 110 110 6 T
Diancie

HoopaHoopa
797 Psychic Ghost 600 80 110 60 150 130 70 6 T
Confined

HoopaHoopa
798 Psychic Dark 680 80 160 60 170 130 80 6 T
Unbound

799 Volcanion Fire Water 600 80 110 120 130 90 70 6 T

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 8/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Column Names

In [15]: pokemon_df.columns

Out[15]: Index(['Name', 'Type 1', 'Type 2', 'Total', 'HP', 'Attack', 'Defense',

'Sp. Atk', 'Sp. Def', 'Speed', 'Generation', 'Legendary'],

dtype='object')

In [16]: pokemon_df.columns.to_list()

Out[16]: ['Name',

'Type 1',

'Type 2',

'Total',

'HP',

'Attack',

'Defense',

'Sp. Atk',

'Sp. Def',

'Speed',

'Generation',

'Legendary']

In [ ]:

In [ ]:

In [ ]:

Rename columns

In [17]: # dataframe with no headers

pokemon_v2_df = pd.read_csv(r'C:\Vivek\Misc\Simplilearn\Pokemon v2.csv', header= None)

pokemon_v2_df

Out[17]: 0 1 2 3 4 5 6 7 8 9 10 11

0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 False

1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 False

2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 False

3 VenusaurMega Venusaur Grass Poison 625 80 100 123 122 120 80 1 False

4 Charmander Fire NaN 309 39 52 43 60 50 65 1 False

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

795 Diancie Rock Fairy 600 50 100 150 100 150 50 6 True

796 DiancieMega Diancie Rock Fairy 700 50 160 110 160 110 110 6 True

797 HoopaHoopa Confined Psychic Ghost 600 80 110 60 150 130 70 6 True

798 HoopaHoopa Unbound Psychic Dark 680 80 160 60 170 130 80 6 True

799 Volcanion Fire Water 600 80 110 120 130 90 70 6 True

800 rows × 12 columns

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 9/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

In [18]: new_names = []

for i in range(1,13):

new_names.append('Column_' + str(i))

new_names

Out[18]: ['Column_1',

'Column_2',

'Column_3',

'Column_4',

'Column_5',

'Column_6',

'Column_7',

'Column_8',

'Column_9',

'Column_10',

'Column_11',

'Column_12']

In [19]: # Change all the columns to the new_names list values


pokemon_v2_df.columns = new_names
pokemon_v2_df

Out[19]: Column_1 Column_2 Column_3 Column_4 Column_5 Column_6 Column_7 Column_8 Colum

0 Bulbasaur Grass Poison 318 45 49 49 65

1 Ivysaur Grass Poison 405 60 62 63 80

2 Venusaur Grass Poison 525 80 82 83 100

VenusaurMega
3 Grass Poison 625 80 100 123 122
Venusaur

4 Charmander Fire NaN 309 39 52 43 60

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

795 Diancie Rock Fairy 600 50 100 150 100

DiancieMega
796 Rock Fairy 700 50 160 110 160
Diancie

HoopaHoopa
797 Psychic Ghost 600 80 110 60 150
Confined

HoopaHoopa
798 Psychic Dark 680 80 160 60 170
Unbound

799 Volcanion Fire Water 600 80 110 120 130

800 rows × 12 columns

In [20]: col_dict = dict({'Column_1' : 'My Column'})

col_dict

Out[20]: {'Column_1': 'My Column'}

In [21]: pokemon_v2_df.rename(columns = col_dict)

Out[21]:
localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 10/69
9/8/2021 Completed 005 2021-08-23 to 2021-09-10

My Column Column_2 Column_3 Column_4 Column_5 Column_6 Column_7 Column_8 Colum

0 Bulbasaur Grass Poison 318 45 49 49 65

1 Ivysaur Grass Poison 405 60 62 63 80

2 Venusaur Grass Poison 525 80 82 83 100

VenusaurMega
3 Grass Poison 625 80 100 123 122
Venusaur

4 Charmander Fire NaN 309 39 52 43 60

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

795 Diancie Rock Fairy 600 50 100 150 100

DiancieMega
796 Rock Fairy 700 50 160 110 160
Diancie

HoopaHoopa
797 Psychic Ghost 600 80 110 60 150
Confined

HoopaHoopa
798 Psychic Dark 680 80 160 60 170
Unbound

799 Volcanion Fire Water 600 80 110 120 130

800 rows × 12 columns

Change is not permanent

In [23]: pokemon_v2_df.info()

<class 'pandas.core.frame.DataFrame'>

RangeIndex: 800 entries, 0 to 799

Data columns (total 12 columns):

# Column Non-Null Count Dtype

--- ------ -------------- -----

0 Column_1 800 non-null object

1 Column_2 800 non-null object

2 Column_3 414 non-null object

3 Column_4 800 non-null int64

4 Column_5 800 non-null int64

5 Column_6 800 non-null int64

6 Column_7 800 non-null int64

7 Column_8 800 non-null int64

8 Column_9 800 non-null int64

9 Column_10 800 non-null int64

10 Column_11 800 non-null int64

11 Column_12 800 non-null bool

dtypes: bool(1), int64(8), object(3)

memory usage: 69.7+ KB

In [24]: pokemon_v2_df = pokemon_v2_df.rename(columns = col_dict)

In [25]: pokemon_v2_df.info()

<class 'pandas.core.frame.DataFrame'>

RangeIndex: 800 entries, 0 to 799

Data columns (total 12 columns):

# Column Non-Null Count Dtype

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 11/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10
--- ------ -------------- -----

0 My Column 800 non-null object

1 Column_2 800 non-null object

2 Column_3 414 non-null object

3 Column_4 800 non-null int64

4 Column_5 800 non-null int64

5 Column_6 800 non-null int64

6 Column_7 800 non-null int64

7 Column_8 800 non-null int64

8 Column_9 800 non-null int64

9 Column_10 800 non-null int64

10 Column_11 800 non-null int64

11 Column_12 800 non-null bool

dtypes: bool(1), int64(8), object(3)

memory usage: 69.7+ KB

Using the inplace option

In [26]: pokemon_v2_df.rename(columns = {'Column_2' : 'My new column' }, inplace = True)

In [27]: pokemon_v2_df.info()

<class 'pandas.core.frame.DataFrame'>

RangeIndex: 800 entries, 0 to 799

Data columns (total 12 columns):

# Column Non-Null Count Dtype

--- ------ -------------- -----

0 My Column 800 non-null object

1 My new column 800 non-null object

2 Column_3 414 non-null object

3 Column_4 800 non-null int64

4 Column_5 800 non-null int64

5 Column_6 800 non-null int64

6 Column_7 800 non-null int64

7 Column_8 800 non-null int64

8 Column_9 800 non-null int64

9 Column_10 800 non-null int64

10 Column_11 800 non-null int64

11 Column_12 800 non-null bool

dtypes: bool(1), int64(8), object(3)

memory usage: 69.7+ KB

In [ ]:

Changing the index

Reset the index


https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.reset_index.html

Change the index to another column


https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.set_index.html

In [31]: pokemon_df

Out[31]: Type Sp. Sp.


Name Type 1 Total HP Attack Defense Speed Generation Legenda
2 Atk Def

0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 Fa

1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 Fa

2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 Fa

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 12/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Type Sp. Sp.


Name Type 1 Total HP Attack Defense Speed Generation Legenda
2 Atk Def

VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur

4 Charmander Fire NaN 309 39 52 43 60 50 65 1 Fa

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

795 Diancie Rock Fairy 600 50 100 150 100 150 50 6 T

DiancieMega
796 Rock Fairy 700 50 160 110 160 110 110 6 T
Diancie

HoopaHoopa
797 Psychic Ghost 600 80 110 60 150 130 70 6 T
Confined

HoopaHoopa
798 Psychic Dark 680 80 160 60 170 130 80 6 T
Unbound

799 Volcanion Fire Water 600 80 110 120 130 90 70 6 T

800 rows × 12 columns

In [34]: pokemon_df = pd.read_csv('Pokemon.csv')

pokemon_df.reset_index(inplace = True) # Make the change permanent

In [35]: pokemon_df

Out[35]: Type Sp. Sp.


index Name Type 1 Total HP Attack Defense Speed Generation
2 Atk Def

0 0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1

1 1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1

2 2 Venusaur Grass Poison 525 80 82 83 100 100 80 1

VenusaurMega
3 3 Grass Poison 625 80 100 123 122 120 80 1
Venusaur

4 4 Charmander Fire NaN 309 39 52 43 60 50 65 1

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

795 795 Diancie Rock Fairy 600 50 100 150 100 150 50 6

DiancieMega
796 796 Rock Fairy 700 50 160 110 160 110 110 6
Diancie

HoopaHoopa
797 797 Psychic Ghost 600 80 110 60 150 130 70 6
Confined

HoopaHoopa
798 798 Psychic Dark 680 80 160 60 170 130 80 6
Unbound

799 799 Volcanion Fire Water 600 80 110 120 130 90 70 6

800 rows × 13 columns

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 13/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

In [36]: # Drop the old index, while creating a new one

pokemon_df = pd.read_csv('Pokemon.csv')

pokemon_df.reset_index(inplace = True, drop = True) # drop the original index

pokemon_df

Out[36]: Type Sp. Sp.


Name Type 1 Total HP Attack Defense Speed Generation Legenda
2 Atk Def

0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 Fa

1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 Fa

2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 Fa

VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur

4 Charmander Fire NaN 309 39 52 43 60 50 65 1 Fa

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

795 Diancie Rock Fairy 600 50 100 150 100 150 50 6 T

DiancieMega
796 Rock Fairy 700 50 160 110 160 110 110 6 T
Diancie

HoopaHoopa
797 Psychic Ghost 600 80 110 60 150 130 70 6 T
Confined

HoopaHoopa
798 Psychic Dark 680 80 160 60 170 130 80 6 T
Unbound

799 Volcanion Fire Water 600 80 110 120 130 90 70 6 T

800 rows × 12 columns

In [37]: pokemon_df.set_index('Name', inplace = True)

pokemon_df

Out[37]: Type Sp. Sp.


Type 1 Total HP Attack Defense Speed Generation Legendary
2 Atk Def

Name

Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 False

Ivysaur Grass Poison 405 60 62 63 80 80 60 1 False

Venusaur Grass Poison 525 80 82 83 100 100 80 1 False

VenusaurMega
Grass Poison 625 80 100 123 122 120 80 1 False
Venusaur

Charmander Fire NaN 309 39 52 43 60 50 65 1 False

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

Diancie Rock Fairy 600 50 100 150 100 150 50 6 True

DiancieMega
Rock Fairy 700 50 160 110 160 110 110 6 True
Diancie

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 14/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Type Sp. Sp.


Type 1 Total HP Attack Defense Speed Generation Legendary
2 Atk Def

Name

HoopaHoopa
Psychic Ghost 600 80 110 60 150 130 70 6 True
Confined

HoopaHoopa
Psychic Dark 680 80 160 60 170 130 80 6 True
Unbound

Volcanion Fire Water 600 80 110 120 130 90 70 6 True

800 rows × 11 columns

In [39]: pokemon_df.info()

<class 'pandas.core.frame.DataFrame'>

Index: 800 entries, Bulbasaur to Volcanion

Data columns (total 11 columns):

# Column Non-Null Count Dtype

--- ------ -------------- -----

0 Type 1 800 non-null object

1 Type 2 414 non-null object

2 Total 800 non-null int64

3 HP 800 non-null int64

4 Attack 800 non-null int64

5 Defense 800 non-null int64

6 Sp. Atk 800 non-null int64

7 Sp. Def 800 non-null int64

8 Speed 800 non-null int64

9 Generation 800 non-null int64

10 Legendary 800 non-null bool

dtypes: bool(1), int64(8), object(2)

memory usage: 69.5+ KB

In [40]: pokemon_df.reset_index(inplace = True)

pokemon_df

Out[40]: Type Sp. Sp.


Name Type 1 Total HP Attack Defense Speed Generation Legenda
2 Atk Def

0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 Fa

1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 Fa

2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 Fa

VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur

4 Charmander Fire NaN 309 39 52 43 60 50 65 1 Fa

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

795 Diancie Rock Fairy 600 50 100 150 100 150 50 6 T

DiancieMega
796 Rock Fairy 700 50 160 110 160 110 110 6 T
Diancie

HoopaHoopa
797 Psychic Ghost 600 80 110 60 150 130 70 6 T
Confined

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 15/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Type Sp. Sp.


Name Type 1 Total HP Attack Defense Speed Generation Legenda
2 Atk Def

HoopaHoopa
798 Psychic Dark 680 80 160 60 170 130 80 6 T
Unbound

799 Volcanion Fire Water 600 80 110 120 130 90 70 6 T

800 rows × 12 columns

frequency tables

In [41]: # Get the original dataframe again

pokemon_df = pd.read_csv('Pokemon.csv')

pokemon_df

Out[41]: Type Sp. Sp.


Name Type 1 Total HP Attack Defense Speed Generation Legenda
2 Atk Def

0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 Fa

1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 Fa

2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 Fa

VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur

4 Charmander Fire NaN 309 39 52 43 60 50 65 1 Fa

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

795 Diancie Rock Fairy 600 50 100 150 100 150 50 6 T

DiancieMega
796 Rock Fairy 700 50 160 110 160 110 110 6 T
Diancie

HoopaHoopa
797 Psychic Ghost 600 80 110 60 150 130 70 6 T
Confined

HoopaHoopa
798 Psychic Dark 680 80 160 60 170 130 80 6 T
Unbound

799 Volcanion Fire Water 600 80 110 120 130 90 70 6 T

800 rows × 12 columns

In [42]: pokemon_df['Generation'].value_counts()

Out[42]: 1 166

5 165

3 160

4 121

2 106

6 82

Name: Generation, dtype: int64

In [43]: pokemon_df['Generation'].value_counts(sort = False) # Sort by column values not frequen

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 16/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Out[43]: 1 166

2 106

3 160

4 121

5 165

6 82

Name: Generation, dtype: int64

By default, value_counts, drops the missing values,


To count the missing values, add the argument dropna = False

In [45]: pokemon_df.info()

<class 'pandas.core.frame.DataFrame'>

RangeIndex: 800 entries, 0 to 799

Data columns (total 12 columns):

# Column Non-Null Count Dtype

--- ------ -------------- -----

0 Name 800 non-null object

1 Type 1 800 non-null object

2 Type 2 414 non-null object

3 Total 800 non-null int64

4 HP 800 non-null int64

5 Attack 800 non-null int64

6 Defense 800 non-null int64

7 Sp. Atk 800 non-null int64

8 Sp. Def 800 non-null int64

9 Speed 800 non-null int64

10 Generation 800 non-null int64

11 Legendary 800 non-null bool

dtypes: bool(1), int64(8), object(3)

memory usage: 69.7+ KB

In [46]: pokemon_df['Type 2'].value_counts()

Out[46]: Flying 97

Ground 35

Poison 34

Psychic 33

Fighting 26

Grass 25

Fairy 23

Steel 22

Dark 20

Dragon 18

Ghost 14

Water 14

Rock 14

Ice 14

Fire 12

Electric 6

Normal 4

Bug 3

Name: Type 2, dtype: int64

In [47]: pokemon_df['Type 2'].value_counts(dropna = False)

Out[47]: NaN 386

Flying 97

Ground 35

Poison 34

Psychic 33

Fighting 26

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 17/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10
Grass 25

Fairy 23

Steel 22

Dark 20

Dragon 18

Ghost 14

Water 14

Rock 14

Ice 14

Fire 12

Electric 6

Normal 4

Bug 3

Name: Type 2, dtype: int64

In [48]: # Get percentage by columns

pokemon_df['Generation'].value_counts(sort = False, normalize = True)

Out[48]: 1 0.20750

2 0.13250

3 0.20000

4 0.15125

5 0.20625

6 0.10250

Name: Generation, dtype: float64

In [49]: pokemon_df['Generation'].value_counts(sort = False, normalize = True) * 100 # to get as

Out[49]: 1 20.750

2 13.250

3 20.000

4 15.125

5 20.625

6 10.250

Name: Generation, dtype: float64

In [53]: pokemon_df[['Type 1', 'Generation']].value_counts()

Out[53]: Type 1 Generation

Water 1 31

3 27

Normal 1 24

5 19

Water 5 18

..

Fairy 1 2

Ice 1 2

Ghost 2 1

Fairy 4 1

Poison 2 1

Length: 98, dtype: int64

Missing Values
Finding Missing Values

In [56]: pokemon_df['Type 2'].isna().sum()

Out[56]: 386

In [57]: pokemon_df.isna().sum()

Out[57]: Name 0

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 18/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10
Type 1 0

Type 2 386

Total 0

HP 0

Attack 0

Defense 0

Sp. Atk 0

Sp. Def 0

Speed 0

Generation 0

Legendary 0

dtype: int64

In [ ]:

Handling Missing Values

In [58]: # Fill a value

pokemon_df['Type 2'].fillna('Missing', inplace = True)

pokemon_df['Type 2'].value_counts()

Out[58]: Missing 386

Flying 97

Ground 35

Poison 34

Psychic 33

Fighting 26

Grass 25

Fairy 23

Steel 22

Dark 20

Dragon 18

Water 14

Rock 14

Ice 14

Ghost 14

Fire 12

Electric 6

Normal 4

Bug 3

Name: Type 2, dtype: int64

In [ ]:

Sorting
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sort_values.html

In [59]: pokemon_df.sort_values(by = 'Name') # This is a view, not permanent, to make it permane

Out[59]: Sp. Sp.


Name Type 1 Type 2 Total HP Attack Defense Speed Generation Leg
Atk Def

510 Abomasnow Grass Ice 494 90 92 75 92 85 60 4

AbomasnowMega
511 Grass Ice 594 90 132 105 132 105 30 4
Abomasnow

68 Abra Psychic Missing 310 25 20 15 105 55 90 1

392 Absol Dark Missing 465 65 130 60 75 60 75 3

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 19/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Sp. Sp.
Name Type 1 Type 2 Total HP Attack Defense Speed Generation Leg
Atk Def

393 AbsolMega Absol Dark Missing 565 65 150 60 115 60 115 3

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

632 Zoroark Dark Missing 510 60 105 60 120 60 105 5

631 Zorua Dark Missing 330 40 65 40 80 40 65 5

46 Zubat Poison Flying 245 40 45 35 30 40 55 1

695 Zweilous Dark Dragon 420 72 85 70 65 70 58 5

Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme

800 rows × 12 columns

In [60]: # Sort descending order

pokemon_df.sort_values(by = 'Name', ascending = False)

Out[60]: Sp. Sp.


Name Type 1 Type 2 Total HP Attack Defense Speed Generation Leg
Atk Def

Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme

695 Zweilous Dark Dragon 420 72 85 70 65 70 58 5

46 Zubat Poison Flying 245 40 45 35 30 40 55 1

631 Zorua Dark Missing 330 40 65 40 80 40 65 5

632 Zoroark Dark Missing 510 60 105 60 120 60 105 5

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

393 AbsolMega Absol Dark Missing 565 65 150 60 115 60 115 3

392 Absol Dark Missing 465 65 130 60 75 60 75 3

68 Abra Psychic Missing 310 25 20 15 105 55 90 1

AbomasnowMega
511 Grass Ice 594 90 132 105 132 105 30 4
Abomasnow

510 Abomasnow Grass Ice 494 90 92 75 92 85 60 4

800 rows × 12 columns

In [61]: # Sort by multiple columns

pokemon_df.sort_values(by = ['Generation', 'Name'])

Out[61]: Sp. Sp.


Name Type 1 Type 2 Total HP Attack Defense Speed Generation Lege
Atk Def

68 Abra Psychic Missing 310 25 20 15 105 55 90 1

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 20/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Sp. Sp.
Name Type 1 Type 2 Total HP Attack Defense Speed Generation Lege
Atk Def

153 Aerodactyl Rock Flying 515 80 105 65 60 75 130 1

AerodactylMega
154 Rock Flying 615 80 135 85 70 95 150 1
Aerodactyl

70 Alakazam Psychic Missing 500 55 50 45 135 95 120 1

AlakazamMega
71 Psychic Missing 590 55 50 65 175 95 150 1
Alakazam

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

734 Vivillon Bug Flying 411 80 52 50 90 50 89 6

799 Volcanion Fire Water 600 80 110 120 130 90 70 6

792 Xerneas Fairy Missing 680 126 131 95 131 98 99 6

793 Yveltal Dark Flying 680 126 131 95 131 98 99 6

Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme

800 rows × 12 columns

In [62]: # Sort by multiple columns, multiple sort orders

pokemon_df.sort_values(by = ['Generation', 'Name'], ascending = [False, True])

Out[62]: Sp. Sp.


Name Type 1 Type 2 Total HP Attack Defense Speed Generation Legen
Atk Def

AegislashBlade
750 Steel Ghost 520 60 150 50 150 50 60 6
Forme

AegislashShield
751 Steel Ghost 520 60 50 150 50 150 60 6
Forme

768 Amaura Rock Ice 362 77 59 50 67 63 46 6

753 Aromatisse Fairy Missing 462 101 72 72 99 89 29 6

769 Aurorus Rock Ice 521 123 77 72 99 92 58 6

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

76 Weepinbell Grass Poison 390 65 90 50 85 45 55 1

118 Weezing Poison Missing 490 65 90 120 85 70 60 1

45 Wigglytuff Normal Fairy 435 140 70 45 85 50 45 1

157 Zapdos Electric Flying 580 90 90 85 125 90 100 1

46 Zubat Poison Flying 245 40 45 35 30 40 55 1

800 rows × 12 columns

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 21/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

In [ ]:

In [ ]:

Exporting a DataFrame
As a csv file

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html

In [ ]: pokemon_df.to_csv('Exported Pokemon.csv')

In [ ]: # I do not want the index to be exported

pokemon_df.to_csv('Exported Pokemon no index.csv', index = False)

to_excel() to export to excel sheet

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_excel.html

In [ ]: pokemon_df.sort_values(by = ['Generation', 'Name'], ascending = [False, True], inplace


pokemon_df

In [ ]: pokemon_df.to_excel('Pokemon.xlsx', sheet_name = 'Sorted', index = False)

In [ ]:

More Stats with Pandas


Mean

In [69]: pokemon_df.mean()

Out[69]: Total 435.10250

HP 69.25875

Attack 79.00125

Defense 73.84250

Sp. Atk 72.82000

Sp. Def 71.90250

Speed 68.27750

Generation 3.32375

Legendary 0.08125

dtype: float64
Median

In [64]: pokemon_df.median()

Out[64]: Total 450.0

HP 65.0

Attack 75.0

Defense 70.0

Sp. Atk 65.0

Sp. Def 70.0

Speed 65.0

Generation 3.0

Legendary 0.0

dtype: float64

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 22/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Mode

In [65]: pokemon_df.mode()

Out[65]: Type Sp. Sp.


Name Type 2 Total HP Attack Defense Speed Generation Le
1 Atk Def

0 Abomasnow Water Missing 600.0 60.0 100.0 70.0 60.0 80.0 50.0 1.0

AbomasnowMega
1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Abomasnow

2 Abra NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

3 Absol NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

4 AbsolMega Absol NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

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

795 Zoroark NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

796 Zorua NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

797 Zubat NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

798 Zweilous NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

Zygarde50%
799 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Forme

800 rows × 12 columns

Percentiles

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.quantile.html

In [66]: pokemon_df['HP'].quantile([0.1, 0.4, 0.6, 0.9]) # Works only for numeric type

Out[66]: 0.1 40.0

0.4 60.0

0.6 71.0

0.9 100.0

Name: HP, dtype: float64

In [ ]:

In [ ]:

In [ ]:

Aggregation Functions
Groupby

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html

In [70]: pokemon_df[['Generation', 'Type 1', 'HP', 'Attack']].groupby(by = ['Generation', 'Type

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 23/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Out[70]: HP Attack

Generation Type 1

1 Bug 56.785714 76.428571

Dragon 64.333333 94.000000

Electric 54.444444 62.000000

Fairy 82.500000 57.500000

Fighting 63.571429 102.857143

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

6 Poison 57.500000 67.500000

Psychic 74.000000 82.800000

Rock 67.111111 90.333333

Steel 56.200000 94.000000

Water 57.600000 68.000000

98 rows × 2 columns

Cross Tab

https://pandas.pydata.org/docs/reference/api/pandas.crosstab.html

In [71]: pd.crosstab(index = pokemon_df['Generation'], columns = pokemon_df['Legendary'], values

Out[71]: Legendary False True

Generation

1 64.612500 98.000000

2 69.613861 103.400000

3 64.626761 81.666667

4 69.740741 100.846154

5 69.353333 96.133333

6 66.189189 87.500000

In [77]: pd.crosstab(index = pokemon_df['Type 1'], columns = pokemon_df['Generation'], values =

Out[77]: Generation 1 2 3 4 5 6

Type 1

Bug 56.785714 59.583333 49.833333 59.000000 59.111111 54.333333

Dark NaN 67.500000 55.833333 80.000000 63.538462 88.333333

Dragon 64.333333 NaN 81.666667 85.500000 93.333333 77.750000

Electric 54.444444 65.714286 60.000000 57.500000 64.750000 57.666667

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 24/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Generation 1 2 3 4 5 6

Type 1

Fairy 82.500000 58.000000 NaN 85.000000 NaN 80.000000

Fighting 63.571429 42.500000 73.200000 60.000000 81.428571 80.000000

Fire 65.857143 66.375000 66.875000 70.000000 83.888889 67.750000

Flying NaN NaN NaN NaN 79.000000 62.500000

Ghost 48.750000 60.000000 46.400000 99.285714 53.200000 61.400000

Grass 66.153846 58.888889 63.000000 74.600000 65.800000 78.800000

Ground 58.125000 81.666667 67.857143 91.500000 81.000000 NaN

Ice 77.500000 60.000000 80.000000 81.666667 63.000000 75.000000

Normal 79.958333 87.200000 68.666667 79.666667 75.421053 60.750000

Poison 64.500000 85.000000 81.000000 67.833333 65.000000 57.500000

Psychic 70.727273 87.714286 58.500000 68.875000 72.214286 74.000000

Rock 56.500000 78.000000 65.250000 60.666667 71.833333 67.111111

Steel NaN 71.666667 65.833333 74.666667 62.750000 56.200000

Water 66.967742 76.388889 72.370370 73.923077 78.722222 57.600000

Pivot Table

https://pandas.pydata.org/docs/reference/api/pandas.pivot_table.html

In [72]: pd.pivot_table(data = pokemon_df, index = ['Generation', 'Type 1'], values = ['HP', 'At

Out[72]: sum count

Attack HP Attack HP

Generation Type 1

1 Bug 1070 795 14 14

Dragon 282 193 3 3

Electric 558 490 9 9

Fairy 115 165 2 2

Fighting 720 445 7 7

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

6 Poison 135 115 2 2

Psychic 414 370 5 5

Rock 813 604 9 9

Steel 470 281 5 5

Water 340 288 5 5

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 25/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

98 rows × 4 columns

In [ ]:

Slicing with Pandas

Subsetting Particular Columns


In [78]: pokemon_df = pd.read_csv('Pokemon.csv')

pokemon_df

Out[78]: Type Sp. Sp.


Name Type 1 Total HP Attack Defense Speed Generation Legenda
2 Atk Def

0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 Fa

1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 Fa

2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 Fa

VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur

4 Charmander Fire NaN 309 39 52 43 60 50 65 1 Fa

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

795 Diancie Rock Fairy 600 50 100 150 100 150 50 6 T

DiancieMega
796 Rock Fairy 700 50 160 110 160 110 110 6 T
Diancie

HoopaHoopa
797 Psychic Ghost 600 80 110 60 150 130 70 6 T
Confined

HoopaHoopa
798 Psychic Dark 680 80 160 60 170 130 80 6 T
Unbound

799 Volcanion Fire Water 600 80 110 120 130 90 70 6 T

800 rows × 12 columns

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 26/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

In [79]: cols_to_keep = ['Name', 'Type 1', 'Total', 'HP']

rel_df = pokemon_df[cols_to_keep]
rel_df

Out[79]: Name Type 1 Total HP

0 Bulbasaur Grass 318 45

1 Ivysaur Grass 405 60

2 Venusaur Grass 525 80

3 VenusaurMega Venusaur Grass 625 80

4 Charmander Fire 309 39

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

795 Diancie Rock 600 50

796 DiancieMega Diancie Rock 700 50

797 HoopaHoopa Confined Psychic 600 80

798 HoopaHoopa Unbound Psychic 680 80

799 Volcanion Fire 600 80

800 rows × 4 columns

Subsetting Particular Rows


Slice like a list

In [80]: rel_df[:100]

Out[80]: Name Type 1 Total HP

0 Bulbasaur Grass 318 45

1 Ivysaur Grass 405 60

2 Venusaur Grass 525 80

3 VenusaurMega Venusaur Grass 625 80

4 Charmander Fire 309 39

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

95 Grimer Poison 325 80

96 Muk Poison 500 105

97 Shellder Water 305 30

98 Cloyster Water 525 50

99 Gastly Ghost 310 30

100 rows × 4 columns

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 27/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Slice using condition

Boolean Masking

In [ ]: rel_df

In [81]: rel_df['HP'] > 50

Out[81]: 0 False

1 True

2 True

3 True

4 False

...

795 False

796 False

797 True

798 True

799 True

Name: HP, Length: 800, dtype: bool

In [82]: more_50_df = rel_df[rel_df['HP'] > 50]

more_50_df

Out[82]: Name Type 1 Total HP

1 Ivysaur Grass 405 60

2 Venusaur Grass 525 80

3 VenusaurMega Venusaur Grass 625 80

5 Charmeleon Fire 405 58

6 Charizard Fire 534 78

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

793 Yveltal Dark 680 126

794 Zygarde50% Forme Dragon 600 108

797 HoopaHoopa Confined Psychic 600 80

798 HoopaHoopa Unbound Psychic 680 80

799 Volcanion Fire 600 80

589 rows × 4 columns

Muliple Conditions

In [84]: # you cannot use keywords here, use operators like &, | ,~ only

two_cond_df = rel_df[(rel_df['Total'] > 600) & (rel_df['HP'] < 70)]

two_cond_df

Out[84]: Name Type 1 Total HP

306 GardevoirMega Gardevoir Psychic 618 68

527 GalladeMega Gallade Psychic 618 68

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 28/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Name Type 1 Total HP

796 DiancieMega Diancie Rock 700 50

In [85]: type(rel_df['HP'] > 50)

Out[85]: pandas.core.series.Series

In [ ]:

In [ ]:

In [ ]:

In [ ]:

Pandas Series vs Pandas DataFrame


In [86]: type(rel_df['Total'] > 600)

Out[86]: pandas.core.series.Series

In [87]: type(rel_df[['Total']])

Out[87]: pandas.core.frame.DataFrame

In [89]: # Pandas Series

rel_df['Total']

Out[89]: 0 318

1 405

2 525

3 625

4 309

...

795 600

796 700

797 600

798 680

799 600

Name: Total, Length: 800, dtype: int64

In [88]: # Data Frame

rel_df[['Total']]

Out[88]: Total

0 318

1 405

2 525

3 625

4 309

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 29/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Total

... ...

795 600

796 700

797 600

798 680

799 600

800 rows × 1 columns

Advanced Slicing with loc and iloc


Slicing by labels

In [90]: named_pokemon = pokemon_df.set_index('Name')

named_pokemon

Out[90]: Type Sp. Sp.


Type 1 Total HP Attack Defense Speed Generation Legendary
2 Atk Def

Name

Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 False

Ivysaur Grass Poison 405 60 62 63 80 80 60 1 False

Venusaur Grass Poison 525 80 82 83 100 100 80 1 False

VenusaurMega
Grass Poison 625 80 100 123 122 120 80 1 False
Venusaur

Charmander Fire NaN 309 39 52 43 60 50 65 1 False

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

Diancie Rock Fairy 600 50 100 150 100 150 50 6 True

DiancieMega
Rock Fairy 700 50 160 110 160 110 110 6 True
Diancie

HoopaHoopa
Psychic Ghost 600 80 110 60 150 130 70 6 True
Confined

HoopaHoopa
Psychic Dark 680 80 160 60 170 130 80 6 True
Unbound

Volcanion Fire Water 600 80 110 120 130 90 70 6 True

800 rows × 11 columns

loc() - row labels, column labels

In [91]: subset_name = named_pokemon.loc['Venusaur': 'Diancie', 'Type 1' : 'Defense']

subset_name

Out[91]:
localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 30/69
9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Type 1 Type 2 Total HP Attack Defense

Name

Venusaur Grass Poison 525 80 82 83

VenusaurMega Venusaur Grass Poison 625 80 100 123

Charmander Fire NaN 309 39 52 43

Charmeleon Fire NaN 405 58 64 58

Charizard Fire Flying 534 78 84 78

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

Noivern Flying Dragon 535 85 70 80

Xerneas Fairy NaN 680 126 131 95

Yveltal Dark Flying 680 126 131 95

Zygarde50% Forme Dragon Ground 600 108 100 121

Diancie Rock Fairy 600 50 100 150

794 rows × 6 columns

All rows, but only certain columns

In [92]: subset_name = named_pokemon.loc[: , 'Type 1' : 'Defense']

subset_name

Out[92]: Type 1 Type 2 Total HP Attack Defense

Name

Bulbasaur Grass Poison 318 45 49 49

Ivysaur Grass Poison 405 60 62 63

Venusaur Grass Poison 525 80 82 83

VenusaurMega Venusaur Grass Poison 625 80 100 123

Charmander Fire NaN 309 39 52 43

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

Diancie Rock Fairy 600 50 100 150

DiancieMega Diancie Rock Fairy 700 50 160 110

HoopaHoopa Confined Psychic Ghost 600 80 110 60

HoopaHoopa Unbound Psychic Dark 680 80 160 60

Volcanion Fire Water 600 80 110 120

800 rows × 6 columns

All columns, only specific rows

In [93]: subset_name = named_pokemon.loc['Venusaur': 'Diancie']

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 31/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

subset_name

Out[93]: Sp. Sp.


Type 1 Type 2 Total HP Attack Defense Speed Generation Legendary
Atk Def

Name

Venusaur Grass Poison 525 80 82 83 100 100 80 1 False

VenusaurMega
Grass Poison 625 80 100 123 122 120 80 1 False
Venusaur

Charmander Fire NaN 309 39 52 43 60 50 65 1 False

Charmeleon Fire NaN 405 58 64 58 80 65 80 1 False

Charizard Fire Flying 534 78 84 78 109 85 100 1 False

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

Noivern Flying Dragon 535 85 70 80 97 80 123 6 False

Xerneas Fairy NaN 680 126 131 95 131 98 99 6 True

Yveltal Dark Flying 680 126 131 95 131 98 99 6 True

Zygarde50%
Dragon Ground 600 108 100 121 81 95 95 6 True
Forme

Diancie Rock Fairy 600 50 100 150 100 150 50 6 True

794 rows × 11 columns

In [94]: type1_index = pokemon_df.set_index('Type 1')

type1_index

Out[94]: Type Sp. Sp.


Name Total HP Attack Defense Speed Generation Legendary
2 Atk Def

Type 1

Grass Bulbasaur Poison 318 45 49 49 65 65 45 1 False

Grass Ivysaur Poison 405 60 62 63 80 80 60 1 False

Grass Venusaur Poison 525 80 82 83 100 100 80 1 False

VenusaurMega
Grass Poison 625 80 100 123 122 120 80 1 False
Venusaur

Fire Charmander NaN 309 39 52 43 60 50 65 1 False

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

Rock Diancie Fairy 600 50 100 150 100 150 50 6 True

DiancieMega
Rock Fairy 700 50 160 110 160 110 110 6 True
Diancie

HoopaHoopa
Psychic Ghost 600 80 110 60 150 130 70 6 True
Confined

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 32/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Type Sp. Sp.


Name Total HP Attack Defense Speed Generation Legendary
2 Atk Def

Type 1

HoopaHoopa
Psychic Dark 680 80 160 60 170 130 80 6 True
Unbound

Fire Volcanion Water 600 80 110 120 130 90 70 6 True

800 rows × 11 columns

In [95]: type1_index.loc['Fire']

Out[95]: Sp. Sp.


Name Type 2 Total HP Attack Defense Speed Generation Legenda
Atk Def

Type
1

Fire Charmander NaN 309 39 52 43 60 50 65 1 Fa

Fire Charmeleon NaN 405 58 64 58 80 65 80 1 Fa

Fire Charizard Flying 534 78 84 78 109 85 100 1 Fa

CharizardMega
Fire Dragon 634 78 130 111 130 85 100 1 Fa
Charizard X

CharizardMega
Fire Flying 634 78 104 78 159 115 100 1 Fa
Charizard Y

Fire Vulpix NaN 299 38 41 40 50 65 65 1 Fa

Fire Ninetales NaN 505 73 76 75 81 100 100 1 Fa

Fire Growlithe NaN 350 55 70 45 70 50 60 1 Fa

Fire Arcanine NaN 555 90 110 80 100 80 95 1 Fa

Fire Ponyta NaN 410 50 85 55 65 65 90 1 Fa

Fire Rapidash NaN 500 65 100 70 80 80 105 1 Fa

Fire Magmar NaN 495 65 95 57 100 85 93 1 Fa

Fire Flareon NaN 525 65 130 60 95 110 65 1 Fa

Fire Moltres Flying 580 90 100 90 125 85 90 1 Tr

Fire Cyndaquil NaN 309 39 52 43 60 50 65 2 Fa

Fire Quilava NaN 405 58 64 58 80 65 80 2 Fa

Fire Typhlosion NaN 534 78 84 78 109 85 100 2 Fa

Fire Slugma NaN 250 40 40 40 70 40 20 2 Fa

Fire Magcargo Rock 410 50 50 120 80 80 30 2 Fa

Fire Magby NaN 365 45 75 37 70 55 83 2 Fa

Fire Entei NaN 580 115 115 85 90 75 100 2 Tr

Fire Ho-oh Flying 680 106 130 90 110 154 90 2 Tr

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 33/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Sp. Sp.
Name Type 2 Total HP Attack Defense Speed Generation Legenda
Atk Def

Type
1

Fire Torchic NaN 310 45 60 40 70 50 45 3 Fa

Fire Combusken Fighting 405 60 85 60 85 60 55 3 Fa

Fire Blaziken Fighting 530 80 120 70 110 70 80 3 Fa

BlazikenMega
Fire Fighting 630 80 160 80 130 80 100 3 Fa
Blaziken

Fire Numel Ground 305 60 60 40 65 45 35 3 Fa

Fire Camerupt Ground 460 70 100 70 105 75 40 3 Fa

CameruptMega
Fire Ground 560 70 120 100 145 105 20 3 Fa
Camerupt

Fire Torkoal NaN 470 70 85 140 85 70 20 3 Fa

Fire Chimchar NaN 309 44 58 44 58 44 61 4 Fa

Fire Monferno Fighting 405 64 78 52 78 52 81 4 Fa

Fire Infernape Fighting 534 76 104 71 104 71 108 4 Fa

Fire Magmortar NaN 540 75 95 67 125 95 83 4 Fa

Fire Heatran Steel 600 91 90 106 130 106 77 4 Tr

Fire Tepig NaN 308 65 63 45 45 45 45 5 Fa

Fire Pignite Fighting 418 90 93 55 70 55 55 5 Fa

Fire Emboar Fighting 528 110 123 65 100 65 65 5 Fa

Fire Pansear NaN 316 50 53 48 53 48 64 5 Fa

Fire Simisear NaN 498 75 98 63 98 63 101 5 Fa

Fire Darumaka NaN 315 70 90 45 15 45 50 5 Fa

DarmanitanStandard
Fire NaN 480 105 140 55 30 55 95 5 Fa
Mode

DarmanitanZen
Fire Psychic 540 105 30 105 140 105 55 5 Fa
Mode

Fire Heatmor NaN 484 85 97 66 105 66 65 5 Fa

Fire Fennekin NaN 307 40 45 40 62 60 60 6 Fa

Fire Braixen NaN 409 59 59 58 90 70 73 6 Fa

Fire Delphox Psychic 534 75 69 72 114 100 104 6 Fa

Fire Fletchinder Flying 382 62 73 55 56 52 84 6 Fa

Fire Talonflame Flying 499 78 81 71 74 69 126 6 Fa

Fire Litleo Normal 369 62 50 58 73 54 72 6 Fa

Fire Pyroar Normal 507 86 68 72 109 66 106 6 Fa

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 34/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Sp. Sp.
Name Type 2 Total HP Attack Defense Speed Generation Legenda
Atk Def

Type
1

Fire Volcanion Water 600 80 110 120 130 90 70 6 Tr

In [ ]:

In [ ]:

Slicing by position

In [96]: named_pokemon

Out[96]: Type Sp. Sp.


Type 1 Total HP Attack Defense Speed Generation Legendary
2 Atk Def

Name

Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 False

Ivysaur Grass Poison 405 60 62 63 80 80 60 1 False

Venusaur Grass Poison 525 80 82 83 100 100 80 1 False

VenusaurMega
Grass Poison 625 80 100 123 122 120 80 1 False
Venusaur

Charmander Fire NaN 309 39 52 43 60 50 65 1 False

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

Diancie Rock Fairy 600 50 100 150 100 150 50 6 True

DiancieMega
Rock Fairy 700 50 160 110 160 110 110 6 True
Diancie

HoopaHoopa
Psychic Ghost 600 80 110 60 150 130 70 6 True
Confined

HoopaHoopa
Psychic Dark 680 80 160 60 170 130 80 6 True
Unbound

Volcanion Fire Water 600 80 110 120 130 90 70 6 True

800 rows × 11 columns

Use slicers for rows and columns

Start : Stop before : Step (Stride)

In [98]: named_pokemon.iloc[1:20:2, 3:7:2]

Out[98]: HP Defense

Name

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 35/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

HP Defense

Name

Ivysaur 60 63

VenusaurMega Venusaur 80 123

Charmeleon 58 58

CharizardMega Charizard X 78 111

Squirtle 44 65

Blastoise 79 100

Caterpie 45 35

Butterfree 60 50

Kakuna 45 50

BeedrillMega Beedrill 65 40

In [ ]:

Illustration of difference
In [99]: sorted_pokemon_df = pokemon_df.sort_values(by = ['Name'])

sorted_pokemon_df

Out[99]: Sp. Sp.


Name Type 1 Type 2 Total HP Attack Defense Speed Generation Leg
Atk Def

510 Abomasnow Grass Ice 494 90 92 75 92 85 60 4

AbomasnowMega
511 Grass Ice 594 90 132 105 132 105 30 4
Abomasnow

68 Abra Psychic NaN 310 25 20 15 105 55 90 1

392 Absol Dark NaN 465 65 130 60 75 60 75 3

393 AbsolMega Absol Dark NaN 565 65 150 60 115 60 115 3

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

632 Zoroark Dark NaN 510 60 105 60 120 60 105 5

631 Zorua Dark NaN 330 40 65 40 80 40 65 5

46 Zubat Poison Flying 245 40 45 35 30 40 55 1

695 Zweilous Dark Dragon 420 72 85 70 65 70 58 5

Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme

800 rows × 12 columns

In [100… sorted_pokemon_df.loc[5:10]

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 36/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Out[100… Sp. Sp.


Name Type 1 Type 2 Total HP Attack Defense Speed Generation Legenda
Atk Def

5 Charmeleon Fire NaN 405 58 64 58 80 65 80 1 Fa

489 Chatot Normal Flying 411 76 65 45 92 42 91 4 Fa

468 Cherrim Grass NaN 450 70 60 70 87 78 85 4 Fa

467 Cherubi Grass NaN 275 45 35 45 62 53 35 4 Fa

720 Chesnaught Grass Fighting 530 88 107 122 74 75 64 6 Fa

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

42 Vulpix Fire NaN 299 38 41 40 50 65 65 1 Fa

350 Wailmer Water NaN 400 130 70 35 70 35 60 3 Fa

351 Wailord Water NaN 500 170 90 45 90 45 60 3 Fa

400 Walrein Ice Water 530 110 80 90 95 90 65 3 Fa

10 Wartortle Water NaN 405 59 63 80 65 80 58 1 Fa

662 rows × 12 columns

In [ ]:

In [101… sorted_pokemon_df.iloc[5:10]

Out[101… Type Type Sp. Sp.


Name Total HP Attack Defense Speed Generation Legendar
1 2 Atk Def

678 Accelgor Bug NaN 495 80 70 40 100 60 145 5 Fals

AegislashBlade
750 Steel Ghost 520 60 150 50 150 50 60 6 Fals
Forme

AegislashShield
751 Steel Ghost 520 60 50 150 50 150 60 6 Fals
Forme

153 Aerodactyl Rock Flying 515 80 105 65 60 75 130 1 Fals

AerodactylMega
154 Rock Flying 615 80 135 85 70 95 150 1 Fals
Aerodactyl

In [ ]:

In [ ]:

In [ ]:

Calculated Columns
In [102… sorted_pokemon_df

Out[102…
localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 37/69
9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Sp. Sp.
Name Type 1 Type 2 Total HP Attack Defense Speed Generation Leg
Atk Def

510 Abomasnow Grass Ice 494 90 92 75 92 85 60 4

AbomasnowMega
511 Grass Ice 594 90 132 105 132 105 30 4
Abomasnow

68 Abra Psychic NaN 310 25 20 15 105 55 90 1

392 Absol Dark NaN 465 65 130 60 75 60 75 3

393 AbsolMega Absol Dark NaN 565 65 150 60 115 60 115 3

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

632 Zoroark Dark NaN 510 60 105 60 120 60 105 5

631 Zorua Dark NaN 330 40 65 40 80 40 65 5

46 Zubat Poison Flying 245 40 45 35 30 40 55 1

695 Zweilous Dark Dragon 420 72 85 70 65 70 58 5

Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme

800 rows × 12 columns

In [103… sorted_pokemon_df['Attack + Def'] = sorted_pokemon_df['Attack'] + sorted_pokemon_df['De


sorted_pokemon_df

Out[103… Sp. Sp.


Name Type 1 Type 2 Total HP Attack Defense Speed Generation Leg
Atk Def

510 Abomasnow Grass Ice 494 90 92 75 92 85 60 4

AbomasnowMega
511 Grass Ice 594 90 132 105 132 105 30 4
Abomasnow

68 Abra Psychic NaN 310 25 20 15 105 55 90 1

392 Absol Dark NaN 465 65 130 60 75 60 75 3

393 AbsolMega Absol Dark NaN 565 65 150 60 115 60 115 3

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

632 Zoroark Dark NaN 510 60 105 60 120 60 105 5

631 Zorua Dark NaN 330 40 65 40 80 40 65 5

46 Zubat Poison Flying 245 40 45 35 30 40 55 1

695 Zweilous Dark Dragon 420 72 85 70 65 70 58 5

Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme

800 rows × 13 columns

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 38/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

In [104… sorted_pokemon_df.info()

<class 'pandas.core.frame.DataFrame'>

Int64Index: 800 entries, 510 to 794

Data columns (total 13 columns):

# Column Non-Null Count Dtype

--- ------ -------------- -----

0 Name 800 non-null object

1 Type 1 800 non-null object

2 Type 2 414 non-null object

3 Total 800 non-null int64

4 HP 800 non-null int64

5 Attack 800 non-null int64

6 Defense 800 non-null int64

7 Sp. Atk 800 non-null int64

8 Sp. Def 800 non-null int64

9 Speed 800 non-null int64

10 Generation 800 non-null int64

11 Legendary 800 non-null bool

12 Attack + Def 800 non-null int64

dtypes: bool(1), int64(9), object(3)

memory usage: 114.3+ KB

In [105… sorted_pokemon_df['Type 1 and 2'] = sorted_pokemon_df['Type 1'] + ' ' + sorted_pokemon_


sorted_pokemon_df

Out[105… Sp. Sp.


Name Type 1 Type 2 Total HP Attack Defense Speed Generation Leg
Atk Def

510 Abomasnow Grass Ice 494 90 92 75 92 85 60 4

AbomasnowMega
511 Grass Ice 594 90 132 105 132 105 30 4
Abomasnow

68 Abra Psychic NaN 310 25 20 15 105 55 90 1

392 Absol Dark NaN 465 65 130 60 75 60 75 3

393 AbsolMega Absol Dark NaN 565 65 150 60 115 60 115 3

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

632 Zoroark Dark NaN 510 60 105 60 120 60 105 5

631 Zorua Dark NaN 330 40 65 40 80 40 65 5

46 Zubat Poison Flying 245 40 45 35 30 40 55 1

695 Zweilous Dark Dragon 420 72 85 70 65 70 58 5

Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme

800 rows × 14 columns

In [106… sorted_pokemon_df['Type 1 and Speed'] = sorted_pokemon_df['Type 1'] + ' ' + sorted_poke


sorted_pokemon_df

Out[106…

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 39/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Sp. Sp.
Name Type 1 Type 2 Total HP Attack Defense Speed Generation Leg
Atk Def

510 Abomasnow Grass Ice 494 90 92 75 92 85 60 4

AbomasnowMega
511 Grass Ice 594 90 132 105 132 105 30 4
Abomasnow

68 Abra Psychic NaN 310 25 20 15 105 55 90 1

392 Absol Dark NaN 465 65 130 60 75 60 75 3

393 AbsolMega Absol Dark NaN 565 65 150 60 115 60 115 3

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

632 Zoroark Dark NaN 510 60 105 60 120 60 105 5

631 Zorua Dark NaN 330 40 65 40 80 40 65 5

46 Zubat Poison Flying 245 40 45 35 30 40 55 1

695 Zweilous Dark Dragon 420 72 85 70 65 70 58 5

Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme

800 rows × 15 columns

In [107… some_random_list = []

for i in range(800):

some_random_list.append("Random Value" + str(i))

some_random_list

Out[107… ['Random Value0',

'Random Value1',

'Random Value2',

'Random Value3',

'Random Value4',

'Random Value5',

'Random Value6',

'Random Value7',

'Random Value8',

'Random Value9',

'Random Value10',

'Random Value11',

'Random Value12',

'Random Value13',

'Random Value14',

'Random Value15',

'Random Value16',

'Random Value17',

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 40/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

'Random Value18',

'Random Value19',

'Random Value20',

'Random Value21',

'Random Value22',

'Random Value23',

'Random Value24',

'Random Value25',

'Random Value26',

'Random Value27',

'Random Value28',

'Random Value29',

'Random Value30',

'Random Value31',

'Random Value32',

'Random Value33',

'Random Value34',

'Random Value35',

'Random Value36',

'Random Value37',

'Random Value38',

'Random Value39',

'Random Value40',

'Random Value41',

'Random Value42',

'Random Value43',

'Random Value44',

'Random Value45',

'Random Value46',

'Random Value47',

'Random Value48',

'Random Value49',

'Random Value50',

'Random Value51',

'Random Value52',

'Random Value53',

'Random Value54',

'Random Value55',

'Random Value56',

'Random Value57',

'Random Value58',

'Random Value59',

'Random Value60',

'Random Value61',

'Random Value62',

'Random Value63',

'Random Value64',

'Random Value65',

'Random Value66',

'Random Value67',

'Random Value68',

'Random Value69',

'Random Value70',

'Random Value71',

'Random Value72',

'Random Value73',

'Random Value74',

'Random Value75',

'Random Value76',

'Random Value77',

'Random Value78',

'Random Value79',

'Random Value80',

'Random Value81',

'Random Value82',

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 41/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

'Random Value83',

'Random Value84',

'Random Value85',

'Random Value86',

'Random Value87',

'Random Value88',

'Random Value89',

'Random Value90',

'Random Value91',

'Random Value92',

'Random Value93',

'Random Value94',

'Random Value95',

'Random Value96',

'Random Value97',

'Random Value98',

'Random Value99',

'Random Value100',

'Random Value101',

'Random Value102',

'Random Value103',

'Random Value104',

'Random Value105',

'Random Value106',

'Random Value107',

'Random Value108',

'Random Value109',

'Random Value110',

'Random Value111',

'Random Value112',

'Random Value113',

'Random Value114',

'Random Value115',

'Random Value116',

'Random Value117',

'Random Value118',

'Random Value119',

'Random Value120',

'Random Value121',

'Random Value122',

'Random Value123',

'Random Value124',

'Random Value125',

'Random Value126',

'Random Value127',

'Random Value128',

'Random Value129',

'Random Value130',

'Random Value131',

'Random Value132',

'Random Value133',

'Random Value134',

'Random Value135',

'Random Value136',

'Random Value137',

'Random Value138',

'Random Value139',

'Random Value140',

'Random Value141',

'Random Value142',

'Random Value143',

'Random Value144',

'Random Value145',

'Random Value146',

'Random Value147',

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 42/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

'Random Value148',

'Random Value149',

'Random Value150',

'Random Value151',

'Random Value152',

'Random Value153',

'Random Value154',

'Random Value155',

'Random Value156',

'Random Value157',

'Random Value158',

'Random Value159',

'Random Value160',

'Random Value161',

'Random Value162',

'Random Value163',

'Random Value164',

'Random Value165',

'Random Value166',

'Random Value167',

'Random Value168',

'Random Value169',

'Random Value170',

'Random Value171',

'Random Value172',

'Random Value173',

'Random Value174',

'Random Value175',

'Random Value176',

'Random Value177',

'Random Value178',

'Random Value179',

'Random Value180',

'Random Value181',

'Random Value182',

'Random Value183',

'Random Value184',

'Random Value185',

'Random Value186',

'Random Value187',

'Random Value188',

'Random Value189',

'Random Value190',

'Random Value191',

'Random Value192',

'Random Value193',

'Random Value194',

'Random Value195',

'Random Value196',

'Random Value197',

'Random Value198',

'Random Value199',

'Random Value200',

'Random Value201',

'Random Value202',

'Random Value203',

'Random Value204',

'Random Value205',

'Random Value206',

'Random Value207',

'Random Value208',

'Random Value209',

'Random Value210',

'Random Value211',

'Random Value212',

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 43/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10
'Random Value213',

'Random Value214',

'Random Value215',

'Random Value216',

'Random Value217',

'Random Value218',

'Random Value219',

'Random Value220',

'Random Value221',

'Random Value222',

'Random Value223',

'Random Value224',

'Random Value225',

'Random Value226',

'Random Value227',

'Random Value228',

'Random Value229',

'Random Value230',

'Random Value231',

'Random Value232',

'Random Value233',

'Random Value234',

'Random Value235',

'Random Value236',

'Random Value237',

'Random Value238',

'Random Value239',

'Random Value240',

'Random Value241',

'Random Value242',

'Random Value243',

'Random Value244',

'Random Value245',

'Random Value246',

'Random Value247',

'Random Value248',

'Random Value249',

'Random Value250',

'Random Value251',

'Random Value252',

'Random Value253',

'Random Value254',

'Random Value255',

'Random Value256',

'Random Value257',

'Random Value258',

'Random Value259',

'Random Value260',

'Random Value261',

'Random Value262',

'Random Value263',

'Random Value264',

'Random Value265',

'Random Value266',

'Random Value267',

'Random Value268',

'Random Value269',

'Random Value270',

'Random Value271',

'Random Value272',

'Random Value273',

'Random Value274',

'Random Value275',

'Random Value276',

'Random Value277',

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 44/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10
'Random Value278',

'Random Value279',

'Random Value280',

'Random Value281',

'Random Value282',

'Random Value283',

'Random Value284',

'Random Value285',

'Random Value286',

'Random Value287',

'Random Value288',

'Random Value289',

'Random Value290',

'Random Value291',

'Random Value292',

'Random Value293',

'Random Value294',

'Random Value295',

'Random Value296',

'Random Value297',

'Random Value298',

'Random Value299',

'Random Value300',

'Random Value301',

'Random Value302',

'Random Value303',

'Random Value304',

'Random Value305',

'Random Value306',

'Random Value307',

'Random Value308',

'Random Value309',

'Random Value310',

'Random Value311',

'Random Value312',

'Random Value313',

'Random Value314',

'Random Value315',

'Random Value316',

'Random Value317',

'Random Value318',

'Random Value319',

'Random Value320',

'Random Value321',

'Random Value322',

'Random Value323',

'Random Value324',

'Random Value325',

'Random Value326',

'Random Value327',

'Random Value328',

'Random Value329',

'Random Value330',

'Random Value331',

'Random Value332',

'Random Value333',

'Random Value334',

'Random Value335',

'Random Value336',

'Random Value337',

'Random Value338',

'Random Value339',

'Random Value340',

'Random Value341',

'Random Value342',

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 45/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10
'Random Value343',

'Random Value344',

'Random Value345',

'Random Value346',

'Random Value347',

'Random Value348',

'Random Value349',

'Random Value350',

'Random Value351',

'Random Value352',

'Random Value353',

'Random Value354',

'Random Value355',

'Random Value356',

'Random Value357',

'Random Value358',

'Random Value359',

'Random Value360',

'Random Value361',

'Random Value362',

'Random Value363',

'Random Value364',

'Random Value365',

'Random Value366',

'Random Value367',

'Random Value368',

'Random Value369',

'Random Value370',

'Random Value371',

'Random Value372',

'Random Value373',

'Random Value374',

'Random Value375',

'Random Value376',

'Random Value377',

'Random Value378',

'Random Value379',

'Random Value380',

'Random Value381',

'Random Value382',

'Random Value383',

'Random Value384',

'Random Value385',

'Random Value386',

'Random Value387',

'Random Value388',

'Random Value389',

'Random Value390',

'Random Value391',

'Random Value392',

'Random Value393',

'Random Value394',

'Random Value395',

'Random Value396',

'Random Value397',

'Random Value398',

'Random Value399',

'Random Value400',

'Random Value401',

'Random Value402',

'Random Value403',

'Random Value404',

'Random Value405',

'Random Value406',

'Random Value407',

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 46/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10
'Random Value408',

'Random Value409',

'Random Value410',

'Random Value411',

'Random Value412',

'Random Value413',

'Random Value414',

'Random Value415',

'Random Value416',

'Random Value417',

'Random Value418',

'Random Value419',

'Random Value420',

'Random Value421',

'Random Value422',

'Random Value423',

'Random Value424',

'Random Value425',

'Random Value426',

'Random Value427',

'Random Value428',

'Random Value429',

'Random Value430',

'Random Value431',

'Random Value432',

'Random Value433',

'Random Value434',

'Random Value435',

'Random Value436',

'Random Value437',

'Random Value438',

'Random Value439',

'Random Value440',

'Random Value441',

'Random Value442',

'Random Value443',

'Random Value444',

'Random Value445',

'Random Value446',

'Random Value447',

'Random Value448',

'Random Value449',

'Random Value450',

'Random Value451',

'Random Value452',

'Random Value453',

'Random Value454',

'Random Value455',

'Random Value456',

'Random Value457',

'Random Value458',

'Random Value459',

'Random Value460',

'Random Value461',

'Random Value462',

'Random Value463',

'Random Value464',

'Random Value465',

'Random Value466',

'Random Value467',

'Random Value468',

'Random Value469',

'Random Value470',

'Random Value471',

'Random Value472',

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 47/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10
'Random Value473',

'Random Value474',

'Random Value475',

'Random Value476',

'Random Value477',

'Random Value478',

'Random Value479',

'Random Value480',

'Random Value481',

'Random Value482',

'Random Value483',

'Random Value484',

'Random Value485',

'Random Value486',

'Random Value487',

'Random Value488',

'Random Value489',

'Random Value490',

'Random Value491',

'Random Value492',

'Random Value493',

'Random Value494',

'Random Value495',

'Random Value496',

'Random Value497',

'Random Value498',

'Random Value499',

'Random Value500',

'Random Value501',

'Random Value502',

'Random Value503',

'Random Value504',

'Random Value505',

'Random Value506',

'Random Value507',

'Random Value508',

'Random Value509',

'Random Value510',

'Random Value511',

'Random Value512',

'Random Value513',

'Random Value514',

'Random Value515',

'Random Value516',

'Random Value517',

'Random Value518',

'Random Value519',

'Random Value520',

'Random Value521',

'Random Value522',

'Random Value523',

'Random Value524',

'Random Value525',

'Random Value526',

'Random Value527',

'Random Value528',

'Random Value529',

'Random Value530',

'Random Value531',

'Random Value532',

'Random Value533',

'Random Value534',

'Random Value535',

'Random Value536',

'Random Value537',

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 48/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10
'Random Value538',

'Random Value539',

'Random Value540',

'Random Value541',

'Random Value542',

'Random Value543',

'Random Value544',

'Random Value545',

'Random Value546',

'Random Value547',

'Random Value548',

'Random Value549',

'Random Value550',

'Random Value551',

'Random Value552',

'Random Value553',

'Random Value554',

'Random Value555',

'Random Value556',

'Random Value557',

'Random Value558',

'Random Value559',

'Random Value560',

'Random Value561',

'Random Value562',

'Random Value563',

'Random Value564',

'Random Value565',

'Random Value566',

'Random Value567',

'Random Value568',

'Random Value569',

'Random Value570',

'Random Value571',

'Random Value572',

'Random Value573',

'Random Value574',

'Random Value575',

'Random Value576',

'Random Value577',

'Random Value578',

'Random Value579',

'Random Value580',

'Random Value581',

'Random Value582',

'Random Value583',

'Random Value584',

'Random Value585',

'Random Value586',

'Random Value587',

'Random Value588',

'Random Value589',

'Random Value590',

'Random Value591',

'Random Value592',

'Random Value593',

'Random Value594',

'Random Value595',

'Random Value596',

'Random Value597',

'Random Value598',

'Random Value599',

'Random Value600',

'Random Value601',

'Random Value602',

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 49/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10
'Random Value603',

'Random Value604',

'Random Value605',

'Random Value606',

'Random Value607',

'Random Value608',

'Random Value609',

'Random Value610',

'Random Value611',

'Random Value612',

'Random Value613',

'Random Value614',

'Random Value615',

'Random Value616',

'Random Value617',

'Random Value618',

'Random Value619',

'Random Value620',

'Random Value621',

'Random Value622',

'Random Value623',

'Random Value624',

'Random Value625',

'Random Value626',

'Random Value627',

'Random Value628',

'Random Value629',

'Random Value630',

'Random Value631',

'Random Value632',

'Random Value633',

'Random Value634',

'Random Value635',

'Random Value636',

'Random Value637',

'Random Value638',

'Random Value639',

'Random Value640',

'Random Value641',

'Random Value642',

'Random Value643',

'Random Value644',

'Random Value645',

'Random Value646',

'Random Value647',

'Random Value648',

'Random Value649',

'Random Value650',

'Random Value651',

'Random Value652',

'Random Value653',

'Random Value654',

'Random Value655',

'Random Value656',

'Random Value657',

'Random Value658',

'Random Value659',

'Random Value660',

'Random Value661',

'Random Value662',

'Random Value663',

'Random Value664',

'Random Value665',

'Random Value666',

'Random Value667',

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 50/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10
'Random Value668',

'Random Value669',

'Random Value670',

'Random Value671',

'Random Value672',

'Random Value673',

'Random Value674',

'Random Value675',

'Random Value676',

'Random Value677',

'Random Value678',

'Random Value679',

'Random Value680',

'Random Value681',

'Random Value682',

'Random Value683',

'Random Value684',

'Random Value685',

'Random Value686',

'Random Value687',

'Random Value688',

'Random Value689',

'Random Value690',

'Random Value691',

'Random Value692',

'Random Value693',

'Random Value694',

'Random Value695',

'Random Value696',

'Random Value697',

'Random Value698',

'Random Value699',

'Random Value700',

'Random Value701',

'Random Value702',

'Random Value703',

'Random Value704',

'Random Value705',

'Random Value706',

'Random Value707',

'Random Value708',

'Random Value709',

'Random Value710',

'Random Value711',

'Random Value712',

'Random Value713',

'Random Value714',

'Random Value715',

'Random Value716',

'Random Value717',

'Random Value718',

'Random Value719',

'Random Value720',

'Random Value721',

'Random Value722',

'Random Value723',

'Random Value724',

'Random Value725',

'Random Value726',

'Random Value727',

'Random Value728',

'Random Value729',

'Random Value730',

'Random Value731',

'Random Value732',

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 51/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10
'Random Value733',

'Random Value734',

'Random Value735',

'Random Value736',

'Random Value737',

'Random Value738',

'Random Value739',

'Random Value740',

'Random Value741',

'Random Value742',

'Random Value743',

'Random Value744',

'Random Value745',

'Random Value746',

'Random Value747',

'Random Value748',

'Random Value749',

'Random Value750',

'Random Value751',

'Random Value752',

'Random Value753',

'Random Value754',

'Random Value755',

'Random Value756',

'Random Value757',

'Random Value758',

'Random Value759',

'Random Value760',

'Random Value761',

'Random Value762',

'Random Value763',

'Random Value764',

'Random Value765',

'Random Value766',

'Random Value767',

'Random Value768',

'Random Value769',

'Random Value770',

'Random Value771',

'Random Value772',

'Random Value773',

'Random Value774',

'Random Value775',

'Random Value776',

'Random Value777',

'Random Value778',

'Random Value779',

'Random Value780',

'Random Value781',

'Random Value782',

'Random Value783',

'Random Value784',

'Random Value785',

'Random Value786',

'Random Value787',

'Random Value788',

'Random Value789',

'Random Value790',

'Random Value791',

'Random Value792',

'Random Value793',

'Random Value794',

'Random Value795',

'Random Value796',

'Random Value797',

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 52/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10
'Random Value798',

'Random Value799']

In [108… sorted_pokemon_df['New Column'] = some_random_list

sorted_pokemon_df

Out[108…
Sp. Sp.
Name Type 1 Type 2 Total HP Attack Defense Speed Generation Leg
Atk Def

510 Abomasnow Grass Ice 494 90 92 75 92 85 60 4

AbomasnowMega
511 Grass Ice 594 90 132 105 132 105 30 4
Abomasnow

68 Abra Psychic NaN 310 25 20 15 105 55 90 1

392 Absol Dark NaN 465 65 130 60 75 60 75 3

393 AbsolMega Absol Dark NaN 565 65 150 60 115 60 115 3

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

632 Zoroark Dark NaN 510 60 105 60 120 60 105 5

631 Zorua Dark NaN 330 40 65 40 80 40 65 5

46 Zubat Poison Flying 245 40 45 35 30 40 55 1

695 Zweilous Dark Dragon 420 72 85 70 65 70 58 5

Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme

800 rows × 16 columns

In [ ]:

In [ ]:

Working with Multiple Dataframes


Appending (Stacking Vertically)
Concatenation (Stacking Horizontally)
SQL style Merging

Appending
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.append.html?

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 53/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

In [109… num_list = []

sq_list = []

for i in range(1, 11):

num_list.append(i)

sq_list.append(i*i)

sq_dict = dict(zip(num_list, sq_list))

In [110… sq_dict

Out[110… {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}

In [111… num_df = pd.DataFrame(data = sq_dict.values(), index = sq_dict.keys(), columns = ['Squa


num_df

Out[111… Square

1 1

2 4

3 9

4 16

5 25

6 36

7 49

8 64

9 81
localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 54/69
9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Square

10 100

In [112… new_list = []

new_sq = []

for i in range(11, 16):

new_list.append(i)

new_sq.append(i*i)

new_sq_dict = dict(zip(new_list, new_sq))

new_sq_df = pd.DataFrame(data = new_sq_dict.values(), columns = ['Square'])

new_sq_df

Out[112… Square

0 121

1 144

2 169

3 196

4 225

In [113… num_df.append(new_sq_df)

Out[113… Square

1 1

2 4

3 9

4 16

5 25

6 36

7 49

8 64

9 81

10 100

0 121

1 144

2 169

3 196

4 225

In [114… num_df.append(new_sq_df, ignore_index=True)

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 55/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Out[114… Square

0 1

1 4

2 9

3 16

4 25

5 36

6 49

7 64

8 81

9 100

10 121

11 144

12 169

13 196

14 225

In [115… num_df

Out[115… Square

1 1

2 4

3 9

4 16

5 25

6 36

7 49

8 64

9 81

10 100

In [116… new_list = []

new_sq = []

new_cubes = []

for i in range(11, 16):

new_list.append(i)

new_sq.append(i*i)

new_cubes.append(i**3)

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 56/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10
new_sq_df = pd.DataFrame({'Square' : new_sq, 'Cube' : new_cubes}, columns = ['Square',
new_sq_df

Out[116… Square Cube

0 121 1331

1 144 1728

2 169 2197

3 196 2744

4 225 3375

In [117… num_df

Out[117… Square

1 1

2 4

3 9

4 16

5 25

6 36

7 49

8 64

9 81

10 100

In [119… num_df.append(new_sq_df)

Out[119… Square Cube

1 1 NaN

2 4 NaN

3 9 NaN

4 16 NaN

5 25 NaN

6 36 NaN

7 49 NaN

8 64 NaN

9 81 NaN

10 100 NaN

0 121 1331.0

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 57/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Square Cube

1 144 1728.0

2 169 2197.0

3 196 2744.0

4 225 3375.0

In [120… num_df.append(new_sq_df, ignore_index = True)

Out[120… Square Cube

0 1 NaN

1 4 NaN

2 9 NaN

3 16 NaN

4 25 NaN

5 36 NaN

6 49 NaN

7 64 NaN

8 81 NaN

9 100 NaN

10 121 1331.0

11 144 1728.0

12 169 2197.0

13 196 2744.0

14 225 3375.0

In [ ]:

Concatenation
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.concat.html

Besides Append, it can also stack

In [121… num_df = pd.DataFrame(data = sq_dict.values(), index = sq_dict.keys(), columns = ['Squa


num_df

Out[121… Square

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 58/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Square

1 1

2 4

3 9

4 16

5 25

6 36

7 49

8 64

9 81

10 100

In [122… new_list = []

new_cubes = []

for i in range(6, 16):

new_list.append(i)

new_cubes.append(i**3)

cubes_df = pd.DataFrame(data = new_cubes, index = new_list, columns = ['Cube'])

cubes_df

Out[122… Cube

6 216

7 343

8 512

9 729

10 1000

11 1331

12 1728

13 2197

14 2744

15 3375

In [123… sq_cubes_df = pd.concat([num_df, cubes_df])

sq_cubes_df

Out[123… Square Cube

1 1.0 NaN

2 4.0 NaN

3 9.0 NaN

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 59/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Square Cube

4 16.0 NaN

5 25.0 NaN

6 36.0 NaN

7 49.0 NaN

8 64.0 NaN

9 81.0 NaN

10 100.0 NaN

6 NaN 216.0

7 NaN 343.0

8 NaN 512.0

9 NaN 729.0

10 NaN 1000.0

11 NaN 1331.0

12 NaN 1728.0

13 NaN 2197.0

14 NaN 2744.0

15 NaN 3375.0

In [124… sq_cubes_df_ax1 = pd.concat([num_df, cubes_df], axis = 1)

sq_cubes_df_ax1

Out[124… Square Cube

1 1.0 NaN

2 4.0 NaN

3 9.0 NaN

4 16.0 NaN

5 25.0 NaN

6 36.0 216.0

7 49.0 343.0

8 64.0 512.0

9 81.0 729.0

10 100.0 1000.0

11 NaN 1331.0

12 NaN 1728.0

13 NaN 2197.0

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 60/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Square Cube

14 NaN 2744.0

15 NaN 3375.0

In [ ]:

In [ ]:

Merging
SQL Style Merging

In [125… num_df

Out[125… Square

1 1

2 4

3 9

4 16

5 25

6 36

7 49

8 64

9 81

10 100

In [126… cubes_df

Out[126… Cube

6 216

7 343

8 512

9 729

10 1000

11 1331

12 1728

13 2197

14 2744

15 3375

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 61/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

In [127… sq_cubes_df = pd.merge(left = num_df, right = cubes_df, left_index = True, right_index


sq_cubes_df

Out[127… Square Cube

1 1 NaN

2 4 NaN

3 9 NaN

4 16 NaN

5 25 NaN

6 36 216.0

7 49 343.0

8 64 512.0

9 81 729.0

10 100 1000.0

Merging using another Key

In [128… pokemon_df

Out[128… Type Sp. Sp.


Name Type 1 Total HP Attack Defense Speed Generation Legenda
2 Atk Def

0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 Fa

1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 Fa

2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 Fa

VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur

4 Charmander Fire NaN 309 39 52 43 60 50 65 1 Fa

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

795 Diancie Rock Fairy 600 50 100 150 100 150 50 6 T

DiancieMega
796 Rock Fairy 700 50 160 110 160 110 110 6 T
Diancie

HoopaHoopa
797 Psychic Ghost 600 80 110 60 150 130 70 6 T
Confined

HoopaHoopa
798 Psychic Dark 680 80 160 60 170 130 80 6 T
Unbound

799 Volcanion Fire Water 600 80 110 120 130 90 70 6 T

800 rows × 12 columns

In [129… att_df = pokemon_df[pokemon_df['Attack'] < 75][['Name', 'Attack']]

att_df

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 62/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Out[129… Name Attack

0 Bulbasaur 49

1 Ivysaur 62

4 Charmander 52

5 Charmeleon 64

9 Squirtle 48

... ... ...

782 PumpkabooLarge Size 66

783 PumpkabooSuper Size 66

788 Bergmite 69

790 Noibat 30

791 Noivern 70

377 rows × 2 columns

In [130… def_df = pokemon_df[pokemon_df['Defense'] > 80][['Name', 'Defense']].rename(columns = {


def_df

Out[130… Random Name Defense

2 Venusaur 83

3 VenusaurMega Venusaur 123

7 CharizardMega Charizard X 111

11 Blastoise 100

12 BlastoiseMega Blastoise 120

... ... ...

793 Yveltal 95

794 Zygarde50% Forme 121

795 Diancie 150

796 DiancieMega Diancie 110

799 Volcanion 120

267 rows × 2 columns

In [131… inner_df = pd.merge(left = att_df , right = def_df, how = 'inner', left_on = 'Name', ri
inner_df

Out[131… Name Attack Random Name Defense

0 Magneton 60 Magneton 95

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 63/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Name Attack Random Name Defense

1 Shellder 65 Shellder 100

2 Onix 45 Onix 160

3 Cubone 50 Cubone 95

4 Koffing 65 Koffing 95

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

61 KeldeoResolute Forme 72 KeldeoResolute Forme 90

62 AegislashShield Forme 50 AegislashShield Forme 150

63 Clawitzer 73 Clawitzer 88

64 Carbink 50 Carbink 150

65 Bergmite 69 Bergmite 85

66 rows × 4 columns

In [132… att_df2 = pokemon_df[pokemon_df['Attack'] < 75][['Name', 'Attack', 'Defense']]

def_df2 = pokemon_df[pokemon_df['Defense'] > 80][['Name', 'Attack', 'Defense']].rename(


def_df2

Out[132… Random Name Attack Defense

2 Venusaur 82 83

3 VenusaurMega Venusaur 100 123

7 CharizardMega Charizard X 130 111

11 Blastoise 83 100

12 BlastoiseMega Blastoise 103 120

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

793 Yveltal 131 95

794 Zygarde50% Forme 100 121

795 Diancie 100 150

796 DiancieMega Diancie 160 110

799 Volcanion 110 120

267 rows × 3 columns

In [133… att_df2

Out[133… Name Attack Defense

0 Bulbasaur 49 49

1 Ivysaur 62 63

4 Charmander 52 43

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 64/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Name Attack Defense

5 Charmeleon 64 58

9 Squirtle 48 65

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

782 PumpkabooLarge Size 66 70

783 PumpkabooSuper Size 66 70

788 Bergmite 69 85

790 Noibat 30 35

791 Noivern 70 80

377 rows × 3 columns

In [134… inner_df2 = pd.merge(left = att_df2 , right = def_df2, how = 'inner', left_on = 'Name',
inner_df2

Out[134… Name Attack_x Defense_x Random Name Attack_y Defense_y

0 Magneton 60 95 Magneton 60 95

1 Shellder 65 100 Shellder 65 100

2 Onix 45 160 Onix 45 160

3 Cubone 50 95 Cubone 50 95

4 Koffing 65 95 Koffing 65 95

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

61 KeldeoResolute Forme 72 90 KeldeoResolute Forme 72 90

62 AegislashShield Forme 50 150 AegislashShield Forme 50 150

63 Clawitzer 73 88 Clawitzer 73 88

64 Carbink 50 150 Carbink 50 150

65 Bergmite 69 85 Bergmite 69 85

66 rows × 6 columns

In [135… inner_df2 = pd.merge(left = att_df2 , right = def_df2, how = 'inner', left_on = 'Name',
, suffixes = [' : left', ' : right'])

inner_df2

Out[135… Attack : Defense : Attack : Defense :


Name Random Name
left left right right

0 Magneton 60 95 Magneton 60 95

1 Shellder 65 100 Shellder 65 100

2 Onix 45 160 Onix 45 160

3 Cubone 50 95 Cubone 50 95

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 65/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Attack : Defense : Attack : Defense :


Name Random Name
left left right right

4 Koffing 65 95 Koffing 65 95

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

KeldeoResolute KeldeoResolute
61 72 90 72 90
Forme Forme

AegislashShield AegislashShield
62 50 150 50 150
Forme Forme

63 Clawitzer 73 88 Clawitzer 73 88

64 Carbink 50 150 Carbink 50 150

65 Bergmite 69 85 Bergmite 69 85

66 rows × 6 columns

In [136… inner_df2['Diff'] = inner_df2['Attack : left'] - inner_df2['Defense : right']

inner_df2

Out[136… Attack : Defense : Attack : Defense :


Name Random Name Diff
left left right right

0 Magneton 60 95 Magneton 60 95 -35

1 Shellder 65 100 Shellder 65 100 -35

2 Onix 45 160 Onix 45 160 -115

3 Cubone 50 95 Cubone 50 95 -45

4 Koffing 65 95 Koffing 65 95 -30

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

KeldeoResolute KeldeoResolute
61 72 90 72 90 -18
Forme Forme

AegislashShield AegislashShield
62 50 150 50 150 -100
Forme Forme

63 Clawitzer 73 88 Clawitzer 73 88 -15

64 Carbink 50 150 Carbink 50 150 -100

65 Bergmite 69 85 Bergmite 69 85 -16

66 rows × 7 columns

In [ ]:

Applying Functions in Pandas


Why?
How?

In [137… def odd_even(n):

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 66/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10
if n % 2 == 0:

return 'Even'

else:

return 'Odd'

In [138… odd_even(23)

Out[138… 'Odd'

In [139… odd_even(26)

Out[139… 'Even'

In [140… inner_df2

Out[140… Attack : Defense : Attack : Defense :


Name Random Name Diff
left left right right

0 Magneton 60 95 Magneton 60 95 -35

1 Shellder 65 100 Shellder 65 100 -35

2 Onix 45 160 Onix 45 160 -115

3 Cubone 50 95 Cubone 50 95 -45

4 Koffing 65 95 Koffing 65 95 -30

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

KeldeoResolute KeldeoResolute
61 72 90 72 90 -18
Forme Forme

AegislashShield AegislashShield
62 50 150 50 150 -100
Forme Forme

63 Clawitzer 73 88 Clawitzer 73 88 -15

64 Carbink 50 150 Carbink 50 150 -100

65 Bergmite 69 85 Bergmite 69 85 -16

66 rows × 7 columns

In [141… inner_df2['Attack Odd or Even'] = inner_df2['Attack : left'].apply(odd_even)

In [142… inner_df2

Out[142… Attack : Defense : Attack : Defense : Attack Odd


Name Random Name Diff
left left right right or Even

0 Magneton 60 95 Magneton 60 95 -35 Even

1 Shellder 65 100 Shellder 65 100 -35 Odd

2 Onix 45 160 Onix 45 160 -115 Odd

3 Cubone 50 95 Cubone 50 95 -45 Even

4 Koffing 65 95 Koffing 65 95 -30 Odd

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 67/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Attack : Defense : Attack : Defense : Attack Odd


Name Random Name Diff
left left right right or Even

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

KeldeoResolute KeldeoResolute
61 72 90 72 90 -18 Even
Forme Forme

AegislashShield AegislashShield
62 50 150 50 150 -100 Even
Forme Forme

63 Clawitzer 73 88 Clawitzer 73 88 -15 Odd

64 Carbink 50 150 Carbink 50 150 -100 Even

65 Bergmite 69 85 Bergmite 69 85 -16 Odd

66 rows × 8 columns

In [143… inner_df2['Len Name'] = inner_df2['Name'].apply(len)

inner_df2

Out[143… Attack Defense Random Attack : Defense : Attack Odd Len


Name Diff
: left : left Name right right or Even Name

0 Magneton 60 95 Magneton 60 95 -35 Even 8

1 Shellder 65 100 Shellder 65 100 -35 Odd 8

2 Onix 45 160 Onix 45 160 -115 Odd 4

3 Cubone 50 95 Cubone 50 95 -45 Even 6

4 Koffing 65 95 Koffing 65 95 -30 Odd 7

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

KeldeoResolute KeldeoResolute
61 72 90 72 90 -18 Even 20
Forme Forme

AegislashShield AegislashShield
62 50 150 50 150 -100 Even 21
Forme Forme

63 Clawitzer 73 88 Clawitzer 73 88 -15 Odd 9

64 Carbink 50 150 Carbink 50 150 -100 Even 7

65 Bergmite 69 85 Bergmite 69 85 -16 Odd 8

66 rows × 9 columns

In [144… inner_df2['Attact Sqr'] = inner_df2['Attack : left'].apply(lambda x : x ** 2)

inner_df2

Out[144… Attack
Attack Defense Random Attack Defense Len Attact
Name Diff Odd or
: left : left Name : right : right Name Sqr
Even

0 Magneton 60 95 Magneton 60 95 -35 Even 8 3600

1 Shellder 65 100 Shellder 65 100 -35 Odd 8 4225

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 68/69


9/8/2021 Completed 005 2021-08-23 to 2021-09-10

Attack
Attack Defense Random Attack Defense Len Attact
Name Diff Odd or
: left : left Name : right : right Name Sqr
Even

2 Onix 45 160 Onix 45 160 -115 Odd 4 2025

3 Cubone 50 95 Cubone 50 95 -45 Even 6 2500

4 Koffing 65 95 Koffing 65 95 -30 Odd 7 4225

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

KeldeoResolute KeldeoResolute
61 72 90 72 90 -18 Even 20 5184
Forme Forme

AegislashShield AegislashShield
62 50 150 50 150 -100 Even 21 2500
Forme Forme

63 Clawitzer 73 88 Clawitzer 73 88 -15 Odd 9 5329

64 Carbink 50 150 Carbink 50 150 -100 Even 7 2500

65 Bergmite 69 85 Bergmite 69 85 -16 Odd 8 4761

66 rows × 10 columns

localhost:8888/nbconvert/html/Completed 005 2021-08-23 to 2021-09-10.ipynb?download=false 69/69

You might also like