Professional Documents
Culture Documents
Python 3
Python 3
Week 01
Thursday 26 August 2021
Master Pandas
https://pandas.pydata.org/docs/
Read a File
image Source:
Data Source:
CSV file
read_csv
https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html
pokemon_df
VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur
... ... ... ... ... ... ... ... ... ... ... ...
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
In [ ]:
pokemon_v2_df
Out[3]: 0 1 2 3 4 5 6 7 8 9 10 11
3 VenusaurMega Venusaur Grass Poison 625 80 100 123 122 120 80 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
Out[4]: 0 1 2 3 4 5 6 7 8 9 10 11
3 VenusaurMega Venusaur Grass Poison 625 80 100 123 122 120 80 1 False
... ... ... ... ... ... ... ... ... ... ... ... ...
0 1 2 3 4 5 6 7 8 9 10 11
pokemon_txt_df
VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur
... ... ... ... ... ... ... ... ... ... ... ...
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
Excel File
read_excel
https://pandas.pydata.org/docs/reference/api/pandas.read_excel.html
pokemon_excel_df
VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur
... ... ... ... ... ... ... ... ... ... ... ...
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
Basic Operations
Exploring the Data
pokemon_df
VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur
... ... ... ... ... ... ... ... ... ... ... ...
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
info
In [8]: pokemon_df.info()
<class 'pandas.core.frame.DataFrame'>
shape
In [9]: pokemon_df.shape
describe
In [10]: pokemon_df.describe()
Out[10]: Total HP Attack Defense Sp. Atk Sp. Def Speed Generation
count 800 800 414 800.00000 800.000000 800.000000 800.000000 800.000000 800.000000
Type Type
Name Total HP Attack Defense Sp. Atk Sp. Def
1 2
top Mandibuzz Water Flying 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()
VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 False
Venusaur
VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fals
Venusaur
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
BlastoiseMega
12 Water NaN 630 79 103 120 135 115 78 1 Fals
Blastoise
BeedrillMega
19 Bug Poison 495 65 150 40 15 80 145 1 Fals
Beedrill
Tail
Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6 T
Forme
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
Column Names
In [15]: pokemon_df.columns
Out[15]: Index(['Name', 'Type 1', 'Type 2', 'Total', 'HP', 'Attack', 'Defense',
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
pokemon_v2_df
Out[17]: 0 1 2 3 4 5 6 7 8 9 10 11
3 VenusaurMega Venusaur Grass Poison 625 80 100 123 122 120 80 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
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']
Out[19]: Column_1 Column_2 Column_3 Column_4 Column_5 Column_6 Column_7 Column_8 Colum
VenusaurMega
3 Grass Poison 625 80 100 123 122
Venusaur
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
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
VenusaurMega
3 Grass Poison 625 80 100 123 122
Venusaur
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
In [23]: pokemon_v2_df.info()
<class 'pandas.core.frame.DataFrame'>
In [25]: pokemon_v2_df.info()
<class 'pandas.core.frame.DataFrame'>
In [27]: pokemon_v2_df.info()
<class 'pandas.core.frame.DataFrame'>
In [ ]:
In [31]: pokemon_df
VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur
... ... ... ... ... ... ... ... ... ... ... ...
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
In [35]: pokemon_df
VenusaurMega
3 3 Grass Poison 625 80 100 123 122 120 80 1
Venusaur
... ... ... ... ... ... ... ... ... ... ... ... ...
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
pokemon_df = pd.read_csv('Pokemon.csv')
pokemon_df
VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur
... ... ... ... ... ... ... ... ... ... ... ...
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
pokemon_df
Name
VenusaurMega
Grass Poison 625 80 100 123 122 120 80 1 False
Venusaur
... ... ... ... ... ... ... ... ... ... ... ...
DiancieMega
Rock Fairy 700 50 160 110 160 110 110 6 True
Diancie
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
In [39]: pokemon_df.info()
<class 'pandas.core.frame.DataFrame'>
pokemon_df
VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur
... ... ... ... ... ... ... ... ... ... ... ...
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
frequency tables
pokemon_df = pd.read_csv('Pokemon.csv')
pokemon_df
VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur
... ... ... ... ... ... ... ... ... ... ... ...
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
In [42]: pokemon_df['Generation'].value_counts()
Out[42]: 1 166
5 165
3 160
4 121
2 106
6 82
Out[43]: 1 166
2 106
3 160
4 121
5 165
6 82
In [45]: pokemon_df.info()
<class 'pandas.core.frame.DataFrame'>
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
Flying 97
Ground 35
Poison 34
Psychic 33
Fighting 26
Fairy 23
Steel 22
Dark 20
Dragon 18
Ghost 14
Water 14
Rock 14
Ice 14
Fire 12
Electric 6
Normal 4
Bug 3
Out[48]: 1 0.20750
2 0.13250
3 0.20000
4 0.15125
5 0.20625
6 0.10250
Out[49]: 1 20.750
2 13.250
3 20.000
4 15.125
5 20.625
6 10.250
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
Missing Values
Finding Missing Values
Out[56]: 386
In [57]: pokemon_df.isna().sum()
Out[57]: Name 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 [ ]:
pokemon_df['Type 2'].value_counts()
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
In [ ]:
Sorting
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sort_values.html
AbomasnowMega
511 Grass Ice 594 90 132 105 132 105 30 4
Abomasnow
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
Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme
... ... ... ... ... ... ... ... ... ... ... ...
AbomasnowMega
511 Grass Ice 594 90 132 105 132 105 30 4
Abomasnow
Sp. Sp.
Name Type 1 Type 2 Total HP Attack Defense Speed Generation Lege
Atk Def
AerodactylMega
154 Rock Flying 615 80 135 85 70 95 150 1
Aerodactyl
AlakazamMega
71 Psychic Missing 590 55 50 65 175 95 150 1
Alakazam
... ... ... ... ... ... ... ... ... ... ... ...
Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme
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
... ... ... ... ... ... ... ... ... ... ... ...
In [ ]:
In [ ]:
Exporting a DataFrame
As a csv file
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_excel.html
In [ ]:
In [69]: pokemon_df.mean()
HP 69.25875
Attack 79.00125
Defense 73.84250
Speed 68.27750
Generation 3.32375
Legendary 0.08125
dtype: float64
Median
In [64]: pokemon_df.median()
HP 65.0
Attack 75.0
Defense 70.0
Speed 65.0
Generation 3.0
Legendary 0.0
dtype: float64
Mode
In [65]: pokemon_df.mode()
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
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
0.4 60.0
0.6 71.0
0.9 100.0
In [ ]:
In [ ]:
In [ ]:
Aggregation Functions
Groupby
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html
Out[70]: HP Attack
Generation Type 1
98 rows × 2 columns
Cross Tab
https://pandas.pydata.org/docs/reference/api/pandas.crosstab.html
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
Out[77]: Generation 1 2 3 4 5 6
Type 1
Generation 1 2 3 4 5 6
Type 1
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
Attack HP Attack HP
Generation Type 1
98 rows × 4 columns
In [ ]:
pokemon_df
VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur
... ... ... ... ... ... ... ... ... ... ... ...
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
rel_df = pokemon_df[cols_to_keep]
rel_df
In [80]: rel_df[:100]
Boolean Masking
In [ ]: rel_df
Out[81]: 0 False
1 True
2 True
3 True
4 False
...
795 False
796 False
797 True
798 True
799 True
more_50_df
Muliple Conditions
In [84]: # you cannot use keywords here, use operators like &, | ,~ only
two_cond_df
Out[85]: pandas.core.series.Series
In [ ]:
In [ ]:
In [ ]:
In [ ]:
Out[86]: pandas.core.series.Series
In [87]: type(rel_df[['Total']])
Out[87]: pandas.core.frame.DataFrame
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
rel_df[['Total']]
Out[88]: Total
0 318
1 405
2 525
3 625
4 309
Total
... ...
795 600
796 700
797 600
798 680
799 600
named_pokemon
Name
VenusaurMega
Grass Poison 625 80 100 123 122 120 80 1 False
Venusaur
... ... ... ... ... ... ... ... ... ... ... ...
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
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
Name
subset_name
Name
subset_name
Name
VenusaurMega
Grass Poison 625 80 100 123 122 120 80 1 False
Venusaur
... ... ... ... ... ... ... ... ... ... ... ...
Zygarde50%
Dragon Ground 600 108 100 121 81 95 95 6 True
Forme
type1_index
Type 1
VenusaurMega
Grass Poison 625 80 100 123 122 120 80 1 False
Venusaur
... ... ... ... ... ... ... ... ... ... ... ...
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
Type 1
HoopaHoopa
Psychic Dark 680 80 160 60 170 130 80 6 True
Unbound
In [95]: type1_index.loc['Fire']
Type
1
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
Sp. Sp.
Name Type 2 Total HP Attack Defense Speed Generation Legenda
Atk Def
Type
1
BlazikenMega
Fire Fighting 630 80 160 80 130 80 100 3 Fa
Blaziken
CameruptMega
Fire Ground 560 70 120 100 145 105 20 3 Fa
Camerupt
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
Sp. Sp.
Name Type 2 Total HP Attack Defense Speed Generation Legenda
Atk Def
Type
1
In [ ]:
In [ ]:
Slicing by position
In [96]: named_pokemon
Name
VenusaurMega
Grass Poison 625 80 100 123 122 120 80 1 False
Venusaur
... ... ... ... ... ... ... ... ... ... ... ...
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
Out[98]: HP Defense
Name
HP Defense
Name
Ivysaur 60 63
Charmeleon 58 58
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
AbomasnowMega
511 Grass Ice 594 90 132 105 132 105 30 4
Abomasnow
... ... ... ... ... ... ... ... ... ... ... ...
Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme
In [100… sorted_pokemon_df.loc[5:10]
... ... ... ... ... ... ... ... ... ... ... ...
In [ ]:
In [101… sorted_pokemon_df.iloc[5:10]
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
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
AbomasnowMega
511 Grass Ice 594 90 132 105 132 105 30 4
Abomasnow
... ... ... ... ... ... ... ... ... ... ... ...
Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme
AbomasnowMega
511 Grass Ice 594 90 132 105 132 105 30 4
Abomasnow
... ... ... ... ... ... ... ... ... ... ... ...
Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme
In [104… sorted_pokemon_df.info()
<class 'pandas.core.frame.DataFrame'>
AbomasnowMega
511 Grass Ice 594 90 132 105 132 105 30 4
Abomasnow
... ... ... ... ... ... ... ... ... ... ... ...
Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme
Out[106…
Sp. Sp.
Name Type 1 Type 2 Total HP Attack Defense Speed Generation Leg
Atk Def
AbomasnowMega
511 Grass Ice 594 90 132 105 132 105 30 4
Abomasnow
... ... ... ... ... ... ... ... ... ... ... ...
Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme
In [107… some_random_list = []
for i in range(800):
some_random_list
'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',
'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',
'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',
'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',
'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',
'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',
'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',
'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',
'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',
'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',
'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',
'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',
'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',
'Random Value799']
sorted_pokemon_df
Out[108…
Sp. Sp.
Name Type 1 Type 2 Total HP Attack Defense Speed Generation Leg
Atk Def
AbomasnowMega
511 Grass Ice 594 90 132 105 132 105 30 4
Abomasnow
... ... ... ... ... ... ... ... ... ... ... ...
Zygarde50%
794 Dragon Ground 600 108 100 121 81 95 95 6
Forme
In [ ]:
In [ ]:
Appending
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.append.html?
In [109… num_list = []
sq_list = []
num_list.append(i)
sq_list.append(i*i)
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}
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 = []
new_list.append(i)
new_sq.append(i*i)
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
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 = []
new_list.append(i)
new_sq.append(i*i)
new_cubes.append(i**3)
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)
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
Square Cube
1 144 1728.0
2 169 2197.0
3 196 2744.0
4 225 3375.0
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
Out[121… Square
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 = []
new_list.append(i)
new_cubes.append(i**3)
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
sq_cubes_df
1 1.0 NaN
2 4.0 NaN
3 9.0 NaN
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
sq_cubes_df_ax1
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
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
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
In [128… pokemon_df
VenusaurMega
3 Grass Poison 625 80 100 123 122 120 80 1 Fa
Venusaur
... ... ... ... ... ... ... ... ... ... ... ...
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
att_df
0 Bulbasaur 49
1 Ivysaur 62
4 Charmander 52
5 Charmeleon 64
9 Squirtle 48
788 Bergmite 69
790 Noibat 30
791 Noivern 70
2 Venusaur 83
11 Blastoise 100
793 Yveltal 95
In [131… inner_df = pd.merge(left = att_df , right = def_df, how = 'inner', left_on = 'Name', ri
inner_df
0 Magneton 60 Magneton 95
3 Cubone 50 Cubone 95
4 Koffing 65 Koffing 95
63 Clawitzer 73 Clawitzer 88
65 Bergmite 69 Bergmite 85
66 rows × 4 columns
2 Venusaur 82 83
11 Blastoise 83 100
In [133… att_df2
0 Bulbasaur 49 49
1 Ivysaur 62 63
4 Charmander 52 43
5 Charmeleon 64 58
9 Squirtle 48 65
788 Bergmite 69 85
790 Noibat 30 35
791 Noivern 70 80
In [134… inner_df2 = pd.merge(left = att_df2 , right = def_df2, how = 'inner', left_on = 'Name',
inner_df2
0 Magneton 60 95 Magneton 60 95
3 Cubone 50 95 Cubone 50 95
4 Koffing 65 95 Koffing 65 95
63 Clawitzer 73 88 Clawitzer 73 88
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
0 Magneton 60 95 Magneton 60 95
3 Cubone 50 95 Cubone 50 95
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
65 Bergmite 69 85 Bergmite 69 85
66 rows × 6 columns
inner_df2
KeldeoResolute KeldeoResolute
61 72 90 72 90 -18
Forme Forme
AegislashShield AegislashShield
62 50 150 50 150 -100
Forme Forme
66 rows × 7 columns
In [ ]:
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
KeldeoResolute KeldeoResolute
61 72 90 72 90 -18
Forme Forme
AegislashShield AegislashShield
62 50 150 50 150 -100
Forme Forme
66 rows × 7 columns
In [142… inner_df2
KeldeoResolute KeldeoResolute
61 72 90 72 90 -18 Even
Forme Forme
AegislashShield AegislashShield
62 50 150 50 150 -100 Even
Forme Forme
66 rows × 8 columns
inner_df2
... ... ... ... ... ... ... ... ... ...
KeldeoResolute KeldeoResolute
61 72 90 72 90 -18 Even 20
Forme Forme
AegislashShield AegislashShield
62 50 150 50 150 -100 Even 21
Forme Forme
66 rows × 9 columns
inner_df2
Out[144… Attack
Attack Defense Random Attack Defense Len Attact
Name Diff Odd or
: left : left Name : right : right Name Sqr
Even
Attack
Attack Defense Random Attack Defense Len Attact
Name Diff Odd or
: left : left Name : right : right Name Sqr
Even
... ... ... ... ... ... ... ... ... ... ...
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
66 rows × 10 columns