際際滷

際際滷Share a Scribd company logo
Dictionaries, Part 1
INTERMEDIATE PYTHON
Hugo Bowne-Anderson
Data Scientist at DataCamp
INTERMEDIATE PYTHON
List
pop = [30.55, 2.77, 39.21]
countries = ["afghanistan", "albania", "algeria"]
ind_alb = countries.index("albania")
ind_alb
1
pop[ind_alb]
2.77
Not convenient
Not intuitive
INTERMEDIATE PYTHON
Dictionary
pop = [30.55, 2.77, 39.21]
countries = ["afghanistan", "albania", "algeria"]
...
{ }
INTERMEDIATE PYTHON
Dictionary
pop = [30.55, 2.77, 39.21]
countries = ["afghanistan", "albania", "algeria"]
...
{"afghanistan":30.55, }
INTERMEDIATE PYTHON
Dictionary
pop = [30.55, 2.77, 39.21]
countries = ["afghanistan", "albania", "algeria"]
...
world = {"afghanistan":30.55, "albania":2.77, "algeria":39.21}
world["albania"]
2.77
Let's practice!
INTERMEDIATE PYTHON
Dictionaries, Part 2
INTERMEDIATE PYTHON
Hugo Bowne-Anderson
Data Scientist at DataCamp
INTERMEDIATE PYTHON
Recap
world = {"afghanistan":30.55, "albania":2.77, "algeria":39.21}
world["albania"]
2.77
world = {"afghanistan":30.55, "albania":2.77,
"algeria":39.21, "albania":2.81}
world
{'afghanistan': 30.55, 'albania': 2.81, 'algeria': 39.21}
INTERMEDIATE PYTHON
Recap
Keys have to be "immutable" objects
{0:"hello", True:"dear", "two":"world"}
{0: 'hello', True: 'dear', 'two': 'world'}
{["just", "to", "test"]: "value"}
TypeError: unhashable type: 'list'
INTERMEDIATE PYTHON
Principality of Sealand
Source: Wikipedia
1
INTERMEDIATE PYTHON
Dictionary
world["sealand"] = 0.000027
world
{'afghanistan': 30.55, 'albania': 2.81,
'algeria': 39.21, 'sealand': 2.7e-05}
"sealand" in world
True
INTERMEDIATE PYTHON
Dictionary
world["sealand"] = 0.000028
world
{'afghanistan': 30.55, 'albania': 2.81,
'algeria': 39.21, 'sealand': 2.8e-05}
del(world["sealand"])
world
{'afghanistan': 30.55, 'albania': 2.81, 'algeria': 39.21}
INTERMEDIATE PYTHON
List vs Dictionary
INTERMEDIATE PYTHON
List vs Dictionary
INTERMEDIATE PYTHON
List vs Dictionary
INTERMEDIATE PYTHON
List vs Dictionary
INTERMEDIATE PYTHON
List vs Dictionary
INTERMEDIATE PYTHON
List vs Dictionary
INTERMEDIATE PYTHON
List vs Dictionary
INTERMEDIATE PYTHON
List vs Dictionary
Let's practice!
INTERMEDIATE PYTHON
Pandas, Part 1
INTERMEDIATE PYTHON
Hugo Bowne-Anderson
Data Scientist at DataCamp
INTERMEDIATE PYTHON
Tabular dataset examples
INTERMEDIATE PYTHON
Tabular dataset examples
INTERMEDIATE PYTHON
Tabular dataset examples
INTERMEDIATE PYTHON
Datasets in Python
2D Numpy array?
One data type
INTERMEDIATE PYTHON
Datasets in Python
INTERMEDIATE PYTHON
Datasets in Python
pandas!
High level data manipulation tool
Wes McKinney
Built on Numpy
DataFrame
INTERMEDIATE PYTHON
DataFrame
brics
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
INTERMEDIATE PYTHON
DataFrame from Dictionary
dict = {
"country":["Brazil", "Russia", "India", "China", "South Africa"],
"capital":["Brasilia", "Moscow", "New Delhi", "Beijing", "Pretoria"],
"area":[8.516, 17.10, 3.286, 9.597, 1.221]
"population":[200.4, 143.5, 1252, 1357, 52.98] }
keys (column labels)
values (data, column by column)
import pandas as pd
brics = pd.DataFrame(dict)
INTERMEDIATE PYTHON
DataFrame from Dictionary (2)
brics
area capital country population
0 8.516 Brasilia Brazil 200.40
1 17.100 Moscow Russia 143.50
2 3.286 New Delhi India 1252.00
3 9.597 Beijing China 1357.00
4 1.221 Pretoria South Africa 52.98
brics.index = ["BR", "RU", "IN", "CH", "SA"]
brics
area capital country population
BR 8.516 Brasilia Brazil 200.40
RU 17.100 Moscow Russia 143.50
IN 3.286 New Delhi India 1252.00
CH 9.597 Beijing China 1357.00
SA 1.221 Pretoria South Africa 52.98
INTERMEDIATE PYTHON
DataFrame from CSV file
brics.csv
,country,capital,area,population
BR,Brazil,Brasilia,8.516,200.4
RU,Russia,Moscow,17.10,143.5
IN,India,New Delhi,3.286,1252
CH,China,Beijing,9.597,1357
SA,South Africa,Pretoria,1.221,52.98
CSV = comma-separated values
INTERMEDIATE PYTHON
DataFrame from CSV file
brics.csv
,country,capital,area,population
BR,Brazil,Brasilia,8.516,200.4
RU,Russia,Moscow,17.10,143.5
IN,India,New Delhi,3.286,1252
CH,China,Beijing,9.597,1357
SA,South Africa,Pretoria,1.221,52.98
brics = pd.read_csv("path/to/brics.csv")
brics
Unnamed: 0 country capital area population
0 BR Brazil Brasilia 8.516 200.40
1 RU Russia Moscow 17.100 143.50
2 IN India New Delhi 3.286 1252.00
3 CH China Beijing 9.597 1357.00
4 SA South Africa Pretoria 1.221 52.98
INTERMEDIATE PYTHON
DataFrame from CSV file
brics = pd.read_csv("path/to/brics.csv", index_col = 0)
brics
country population area capital
BR Brazil 200 8515767 Brasilia
RU Russia 144 17098242 Moscow
IN India 1252 3287590 New Delhi
CH China 1357 9596961 Beijing
SA South Africa 55 1221037 Pretoria
Let's practice!
INTERMEDIATE PYTHON
Pandas, Part 2
INTERMEDIATE PYTHON
Hugo Bowne-Anderson
Data Scientist at DataCamp
INTERMEDIATE PYTHON
brics
import pandas as pd
brics = pd.read_csv("path/to/brics.csv", index_col = 0)
brics
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
INTERMEDIATE PYTHON
Index and select data
Square brackets
Advanced methods
loc
iloc
INTERMEDIATE PYTHON
Column Access [ ]
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
brics["country"]
BR Brazil
RU Russia
IN India
CH China
SA South Africa
Name: country, dtype: object
INTERMEDIATE PYTHON
Column Access [ ]
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
type(brics["country"])
pandas.core.series.Series
1D labelled array
INTERMEDIATE PYTHON
Column Access [ ]
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
brics[["country"]]
country
BR Brazil
RU Russia
IN India
CH China
SA South Africa
INTERMEDIATE PYTHON
Column Access [ ]
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
type(brics[["country"]])
pandas.core.frame.DataFrame
INTERMEDIATE PYTHON
Column Access [ ]
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
brics[["country", "capital"]]
country capital
BR Brazil Brasilia
RU Russia Moscow
IN India New Delhi
CH China Beijing
SA South Africa Pretoria
INTERMEDIATE PYTHON
Row Access [ ]
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
brics[1:4]
country capital area population
RU Russia Moscow 17.100 143.5
IN India New Delhi 3.286 1252.0
CH China Beijing 9.597 1357.0
INTERMEDIATE PYTHON
Row Access [ ]
country capital area population
BR Brazil Brasilia 8.516 200.40 * 0 *
RU Russia Moscow 17.100 143.50 * 1 *
IN India New Delhi 3.286 1252.00 * 2 *
CH China Beijing 9.597 1357.00 * 3 *
SA South Africa Pretoria 1.221 52.98 * 4 *
brics[1:4]
country capital area population
RU Russia Moscow 17.100 143.5
IN India New Delhi 3.286 1252.0
CH China Beijing 9.597 1357.0
INTERMEDIATE PYTHON
Discussion [ ]
Square brackets: limited functionality
Ideally
2D Numpy arrays
my_array[rows, columns]
pandas
loc (label-based)
iloc (integer position-based)
INTERMEDIATE PYTHON
Row Access loc
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
brics.loc["RU"]
country Russia
capital Moscow
area 17.1
population 143.5
Name: RU, dtype: object
Row as pandas Series
INTERMEDIATE PYTHON
Row Access loc
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
brics.loc[["RU"]]
country capital area population
RU Russia Moscow 17.1 143.5
DataFrame
INTERMEDIATE PYTHON
Row Access loc
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
brics.loc[["RU", "IN", "CH"]]
country capital area population
RU Russia Moscow 17.100 143.5
IN India New Delhi 3.286 1252.0
CH China Beijing 9.597 1357.0
INTERMEDIATE PYTHON
Row & Column loc
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
brics.loc[["RU", "IN", "CH"], ["country", "capital"]]
country capital
RU Russia Moscow
IN India New Delhi
CH China Beijing
INTERMEDIATE PYTHON
Row & Column loc
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
brics.loc[:, ["country", "capital"]]
country capital
BR Brazil Brasilia
RU Russia Moscow
IN India New Delhi
CH China Beijing
SA South Africa Pretoria
INTERMEDIATE PYTHON
Recap
Square brackets
Column access brics[["country", "capital"]]
Row access: only through slicing brics[1:4]
loc (label-based)
Row access brics.loc[["RU", "IN", "CH"]]
Column access brics.loc[:, ["country", "capital"]]
Row & Column access
brics.loc[
["RU", "IN", "CH"],
["country", "capital"]
]
INTERMEDIATE PYTHON
Row Access iloc
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
brics.loc[["RU"]]
country capital area population
RU Russia Moscow 17.1 143.5
brics.iloc[[1]]
country capital area population
RU Russia Moscow 17.1 143.5
INTERMEDIATE PYTHON
Row Access iloc
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
brics.loc[["RU", "IN", "CH"]]
country capital area population
RU Russia Moscow 17.100 143.5
IN India New Delhi 3.286 1252.0
CH China Beijing 9.597 1357.0
INTERMEDIATE PYTHON
Row Access iloc
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
brics.iloc[[1,2,3]]
country capital area population
RU Russia Moscow 17.100 143.5
IN India New Delhi 3.286 1252.0
CH China Beijing 9.597 1357.0
INTERMEDIATE PYTHON
Row & Column iloc
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
brics.loc[["RU", "IN", "CH"], ["country", "capital"]]
country capital
RU Russia Moscow
IN India New Delhi
CH China Beijing
INTERMEDIATE PYTHON
Row & Column iloc
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
brics.iloc[[1,2,3], [0, 1]]
country capital
RU Russia Moscow
IN India New Delhi
CH China Beijing
INTERMEDIATE PYTHON
Row & Column iloc
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
brics.loc[:, ["country", "capital"]]
country capital
BR Brazil Brasilia
RU Russia Moscow
IN India New Delhi
CH China Beijing
SA South Africa Pretoria
INTERMEDIATE PYTHON
Row & Column iloc
country capital area population
BR Brazil Brasilia 8.516 200.40
RU Russia Moscow 17.100 143.50
IN India New Delhi 3.286 1252.00
CH China Beijing 9.597 1357.00
SA South Africa Pretoria 1.221 52.98
brics.iloc[:, [0,1]]
country capital
BR Brazil Brasilia
RU Russia Moscow
IN India New Delhi
CH China Beijing
SA South Africa Pretoria
Let's practice!
INTERMEDIATE PYTHON

More Related Content

chapter2.pdf

  • 1. Dictionaries, Part 1 INTERMEDIATE PYTHON Hugo Bowne-Anderson Data Scientist at DataCamp
  • 2. INTERMEDIATE PYTHON List pop = [30.55, 2.77, 39.21] countries = ["afghanistan", "albania", "algeria"] ind_alb = countries.index("albania") ind_alb 1 pop[ind_alb] 2.77 Not convenient Not intuitive
  • 3. INTERMEDIATE PYTHON Dictionary pop = [30.55, 2.77, 39.21] countries = ["afghanistan", "albania", "algeria"] ... { }
  • 4. INTERMEDIATE PYTHON Dictionary pop = [30.55, 2.77, 39.21] countries = ["afghanistan", "albania", "algeria"] ... {"afghanistan":30.55, }
  • 5. INTERMEDIATE PYTHON Dictionary pop = [30.55, 2.77, 39.21] countries = ["afghanistan", "albania", "algeria"] ... world = {"afghanistan":30.55, "albania":2.77, "algeria":39.21} world["albania"] 2.77
  • 7. Dictionaries, Part 2 INTERMEDIATE PYTHON Hugo Bowne-Anderson Data Scientist at DataCamp
  • 8. INTERMEDIATE PYTHON Recap world = {"afghanistan":30.55, "albania":2.77, "algeria":39.21} world["albania"] 2.77 world = {"afghanistan":30.55, "albania":2.77, "algeria":39.21, "albania":2.81} world {'afghanistan': 30.55, 'albania': 2.81, 'algeria': 39.21}
  • 9. INTERMEDIATE PYTHON Recap Keys have to be "immutable" objects {0:"hello", True:"dear", "two":"world"} {0: 'hello', True: 'dear', 'two': 'world'} {["just", "to", "test"]: "value"} TypeError: unhashable type: 'list'
  • 10. INTERMEDIATE PYTHON Principality of Sealand Source: Wikipedia 1
  • 11. INTERMEDIATE PYTHON Dictionary world["sealand"] = 0.000027 world {'afghanistan': 30.55, 'albania': 2.81, 'algeria': 39.21, 'sealand': 2.7e-05} "sealand" in world True
  • 12. INTERMEDIATE PYTHON Dictionary world["sealand"] = 0.000028 world {'afghanistan': 30.55, 'albania': 2.81, 'algeria': 39.21, 'sealand': 2.8e-05} del(world["sealand"]) world {'afghanistan': 30.55, 'albania': 2.81, 'algeria': 39.21}
  • 22. Pandas, Part 1 INTERMEDIATE PYTHON Hugo Bowne-Anderson Data Scientist at DataCamp
  • 26. INTERMEDIATE PYTHON Datasets in Python 2D Numpy array? One data type
  • 28. INTERMEDIATE PYTHON Datasets in Python pandas! High level data manipulation tool Wes McKinney Built on Numpy DataFrame
  • 29. INTERMEDIATE PYTHON DataFrame brics country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98
  • 30. INTERMEDIATE PYTHON DataFrame from Dictionary dict = { "country":["Brazil", "Russia", "India", "China", "South Africa"], "capital":["Brasilia", "Moscow", "New Delhi", "Beijing", "Pretoria"], "area":[8.516, 17.10, 3.286, 9.597, 1.221] "population":[200.4, 143.5, 1252, 1357, 52.98] } keys (column labels) values (data, column by column) import pandas as pd brics = pd.DataFrame(dict)
  • 31. INTERMEDIATE PYTHON DataFrame from Dictionary (2) brics area capital country population 0 8.516 Brasilia Brazil 200.40 1 17.100 Moscow Russia 143.50 2 3.286 New Delhi India 1252.00 3 9.597 Beijing China 1357.00 4 1.221 Pretoria South Africa 52.98 brics.index = ["BR", "RU", "IN", "CH", "SA"] brics area capital country population BR 8.516 Brasilia Brazil 200.40 RU 17.100 Moscow Russia 143.50 IN 3.286 New Delhi India 1252.00 CH 9.597 Beijing China 1357.00 SA 1.221 Pretoria South Africa 52.98
  • 32. INTERMEDIATE PYTHON DataFrame from CSV file brics.csv ,country,capital,area,population BR,Brazil,Brasilia,8.516,200.4 RU,Russia,Moscow,17.10,143.5 IN,India,New Delhi,3.286,1252 CH,China,Beijing,9.597,1357 SA,South Africa,Pretoria,1.221,52.98 CSV = comma-separated values
  • 33. INTERMEDIATE PYTHON DataFrame from CSV file brics.csv ,country,capital,area,population BR,Brazil,Brasilia,8.516,200.4 RU,Russia,Moscow,17.10,143.5 IN,India,New Delhi,3.286,1252 CH,China,Beijing,9.597,1357 SA,South Africa,Pretoria,1.221,52.98 brics = pd.read_csv("path/to/brics.csv") brics Unnamed: 0 country capital area population 0 BR Brazil Brasilia 8.516 200.40 1 RU Russia Moscow 17.100 143.50 2 IN India New Delhi 3.286 1252.00 3 CH China Beijing 9.597 1357.00 4 SA South Africa Pretoria 1.221 52.98
  • 34. INTERMEDIATE PYTHON DataFrame from CSV file brics = pd.read_csv("path/to/brics.csv", index_col = 0) brics country population area capital BR Brazil 200 8515767 Brasilia RU Russia 144 17098242 Moscow IN India 1252 3287590 New Delhi CH China 1357 9596961 Beijing SA South Africa 55 1221037 Pretoria
  • 36. Pandas, Part 2 INTERMEDIATE PYTHON Hugo Bowne-Anderson Data Scientist at DataCamp
  • 37. INTERMEDIATE PYTHON brics import pandas as pd brics = pd.read_csv("path/to/brics.csv", index_col = 0) brics country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98
  • 38. INTERMEDIATE PYTHON Index and select data Square brackets Advanced methods loc iloc
  • 39. INTERMEDIATE PYTHON Column Access [ ] country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 brics["country"] BR Brazil RU Russia IN India CH China SA South Africa Name: country, dtype: object
  • 40. INTERMEDIATE PYTHON Column Access [ ] country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 type(brics["country"]) pandas.core.series.Series 1D labelled array
  • 41. INTERMEDIATE PYTHON Column Access [ ] country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 brics[["country"]] country BR Brazil RU Russia IN India CH China SA South Africa
  • 42. INTERMEDIATE PYTHON Column Access [ ] country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 type(brics[["country"]]) pandas.core.frame.DataFrame
  • 43. INTERMEDIATE PYTHON Column Access [ ] country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 brics[["country", "capital"]] country capital BR Brazil Brasilia RU Russia Moscow IN India New Delhi CH China Beijing SA South Africa Pretoria
  • 44. INTERMEDIATE PYTHON Row Access [ ] country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 brics[1:4] country capital area population RU Russia Moscow 17.100 143.5 IN India New Delhi 3.286 1252.0 CH China Beijing 9.597 1357.0
  • 45. INTERMEDIATE PYTHON Row Access [ ] country capital area population BR Brazil Brasilia 8.516 200.40 * 0 * RU Russia Moscow 17.100 143.50 * 1 * IN India New Delhi 3.286 1252.00 * 2 * CH China Beijing 9.597 1357.00 * 3 * SA South Africa Pretoria 1.221 52.98 * 4 * brics[1:4] country capital area population RU Russia Moscow 17.100 143.5 IN India New Delhi 3.286 1252.0 CH China Beijing 9.597 1357.0
  • 46. INTERMEDIATE PYTHON Discussion [ ] Square brackets: limited functionality Ideally 2D Numpy arrays my_array[rows, columns] pandas loc (label-based) iloc (integer position-based)
  • 47. INTERMEDIATE PYTHON Row Access loc country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 brics.loc["RU"] country Russia capital Moscow area 17.1 population 143.5 Name: RU, dtype: object Row as pandas Series
  • 48. INTERMEDIATE PYTHON Row Access loc country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 brics.loc[["RU"]] country capital area population RU Russia Moscow 17.1 143.5 DataFrame
  • 49. INTERMEDIATE PYTHON Row Access loc country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 brics.loc[["RU", "IN", "CH"]] country capital area population RU Russia Moscow 17.100 143.5 IN India New Delhi 3.286 1252.0 CH China Beijing 9.597 1357.0
  • 50. INTERMEDIATE PYTHON Row & Column loc country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 brics.loc[["RU", "IN", "CH"], ["country", "capital"]] country capital RU Russia Moscow IN India New Delhi CH China Beijing
  • 51. INTERMEDIATE PYTHON Row & Column loc country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 brics.loc[:, ["country", "capital"]] country capital BR Brazil Brasilia RU Russia Moscow IN India New Delhi CH China Beijing SA South Africa Pretoria
  • 52. INTERMEDIATE PYTHON Recap Square brackets Column access brics[["country", "capital"]] Row access: only through slicing brics[1:4] loc (label-based) Row access brics.loc[["RU", "IN", "CH"]] Column access brics.loc[:, ["country", "capital"]] Row & Column access brics.loc[ ["RU", "IN", "CH"], ["country", "capital"] ]
  • 53. INTERMEDIATE PYTHON Row Access iloc country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 brics.loc[["RU"]] country capital area population RU Russia Moscow 17.1 143.5 brics.iloc[[1]] country capital area population RU Russia Moscow 17.1 143.5
  • 54. INTERMEDIATE PYTHON Row Access iloc country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 brics.loc[["RU", "IN", "CH"]] country capital area population RU Russia Moscow 17.100 143.5 IN India New Delhi 3.286 1252.0 CH China Beijing 9.597 1357.0
  • 55. INTERMEDIATE PYTHON Row Access iloc country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 brics.iloc[[1,2,3]] country capital area population RU Russia Moscow 17.100 143.5 IN India New Delhi 3.286 1252.0 CH China Beijing 9.597 1357.0
  • 56. INTERMEDIATE PYTHON Row & Column iloc country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 brics.loc[["RU", "IN", "CH"], ["country", "capital"]] country capital RU Russia Moscow IN India New Delhi CH China Beijing
  • 57. INTERMEDIATE PYTHON Row & Column iloc country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 brics.iloc[[1,2,3], [0, 1]] country capital RU Russia Moscow IN India New Delhi CH China Beijing
  • 58. INTERMEDIATE PYTHON Row & Column iloc country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 brics.loc[:, ["country", "capital"]] country capital BR Brazil Brasilia RU Russia Moscow IN India New Delhi CH China Beijing SA South Africa Pretoria
  • 59. INTERMEDIATE PYTHON Row & Column iloc country capital area population BR Brazil Brasilia 8.516 200.40 RU Russia Moscow 17.100 143.50 IN India New Delhi 3.286 1252.00 CH China Beijing 9.597 1357.00 SA South Africa Pretoria 1.221 52.98 brics.iloc[:, [0,1]] country capital BR Brazil Brasilia RU Russia Moscow IN India New Delhi CH China Beijing SA South Africa Pretoria