Skip to content

Commit 1b6ab78

Browse files
authored
Add Pandas and Seaborn exercise files
1 parent 8be00ae commit 1b6ab78

File tree

6 files changed

+54
-0
lines changed

6 files changed

+54
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import pandas as pd
2+
3+
df = pd.read_csv('exoplanets_5250_EarthUnits_fixed.csv',index_col=0)
4+
df.to_csv('./docs/day3/exoplanets_5250_EarthUnits.txt', sep='\t',index=True)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import numpy as np
2+
import pandas as pd
3+
4+
df = pd.DataFrame( np.arange(1,13).reshape((4,3)), index=['w','x','y','z'], columns=['a','b','c'] )
5+
print(df)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pandas as pd
2+
import numpy as np
3+
4+
df = pd.read_csv('./docs/day3/exoplanets_5250_EarthUnits_fixed.csv',index_col=0)
5+
print("Before:\n", df['planet_type'].memory_usage(deep=True), '\n')
6+
7+
# Convert planet_type to Categorical
8+
df['planet_type']=df['planet_type'].astype('category')
9+
print("After:\n", df['planet_type'].memory_usage(deep=True))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pandas as pd
2+
import numpy as np
3+
4+
loss_sum = 0
5+
for chunk in pd.read_csv('./docs/day3/global_disaster_response_2018-2024.csv',
6+
chunksize=10000):
7+
loss_sum+=chunk['economic_loss_usd'].sum()
8+
print('total loss over all disasters in this database: $',
9+
np.round(loss_sum/10**9,2), 'billion USD')
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import seaborn as sb
2+
### Uncomment next 2 lines if running directly from cmd line:
3+
import matplotlib
4+
matplotlib.use("TkAgg")
5+
6+
dat = sb.load_dataset('penguins')
7+
g = sb.pairplot(data=dat, corner=True, hue='species')
8+
### Use next 2 lines if running directly at cmd line
9+
g.figure.show()
10+
input("Press any key to exit")
11+
### comment line above, uncomment line below if running from batch script
12+
#g.figure.savefig('penguins_pairplot.png')
13+
### title and format file as desired
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import seaborn as sb
2+
from matplotlib import pyplot as plt
3+
### May need to uncomment next 2 lines if running directly from cmd line:
4+
#import matplotlib
5+
#matplotlib.use("TkAgg")
6+
7+
mpg = sb.load_dataset('mpg')
8+
sb.clustermap(mpg.corr(numeric_only=True), annot=True, fmt=".2f",
9+
cbar_kws={'label':'Correlation Coefficients'})
10+
### Use next line if copying to IDE or running directly at cmd line
11+
plt.show()
12+
### comment line above, uncomment line below if running from batch script
13+
#plt.savefig('mpg_clustermap',format='pdf')
14+
### title and format file as desired

0 commit comments

Comments
 (0)