Top Banner
MERGING DATAFRAMES WITH PANDAS Medals in the Summer Olympics
20

Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

Jun 30, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

MERGING DATAFRAMES WITH PANDAS

Medals in the Summer Olympics

Page 2: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

Merging DataFrames with pandas

Does a host country win more medals?

Page 3: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

Merging DataFrames with pandas

Summer Olympic medalists 1896 to 2008 - IOC COUNTRY CODES.csv

Page 4: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

Merging DataFrames with pandas

Summer Olympic medalists 1896 to 2008 - EDITIONS.tsv

Page 5: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

Merging DataFrames with pandas

summer_1896.csv, summer_1900.csv, …, summer_2008.csv

Page 6: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

Merging DataFrames with pandas

Reminder: loading & merging files● pd.read_csv() (& its many options)

● Looping over files, e.g.,

● [pd.read_csv(f) for f in glob('*.csv')]

● Concatenating & appending, e.g.,

● pd.concat([df1, df2], axis=0)

● df1.append(df2)

Page 7: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

MERGING DATAFRAMES WITH PANDAS

Let’s practice!

Page 8: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

MERGING DATAFRAMES WITH PANDAS

Quantifying performance

Page 9: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

Merging DataFrames with pandas

Medals DataFrame

Page 10: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

Merging DataFrames with pandas

Constructing a pivot table● Apply DataFrame pivot_table() method

● index: column to use as index of pivot table

● values: column(s) to aggregate

● aggfunc: function to apply for aggregation

● columns: categories as columns of pivot table

Page 11: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

Merging DataFrames with pandas

Constructing a pivot table

Page 12: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

Merging DataFrames with pandas

Computing fractions

Page 13: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

MERGING DATAFRAMES WITH PANDAS

Let’s practice!

Page 14: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

MERGING DATAFRAMES WITH PANDAS

Reshaping and plo!ing

Page 15: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

Merging DataFrames with pandas

Reshaping the data

Page 16: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

Merging DataFrames with pandas

Host country data

Page 17: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

Merging DataFrames with pandas

Quantifying influence

Page 18: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

Merging DataFrames with pandas

Graphical summary

Page 19: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

MERGING DATAFRAMES WITH PANDAS

Let’s practice!

Page 20: Medals in the Summer Olympics · 2017-02-18 · Medals in the Summer Olympics. Merging DataFrames with pandas Does a host country win more medals? Merging DataFrames with pandas Summer

MERGING DATAFRAMES WITH PANDAS

Final thoughts