Skip to content

Instantly share code, notes, and snippets.

View DalyaG's full-sized avatar

Dalya Gartzman DalyaG

View GitHub Profile
@DalyaG
DalyaG / pointers_and_references_b_points_to_a.cpp
Last active May 18, 2020 17:28
Pointers and references in C++: what happens when b points to an alias of a
#include <iostream>
using namespace std;
void change_val(int &z);
int main() {
int a = 1;
int *b = &a;
cout << "b points to an alias of the integer a\n";
cout << "a=" << a << ", b=" << b << ", *b=" << *b << endl;
@DalyaG
DalyaG / pointers_and_references_no_p_no_r.cpp
Last active May 18, 2020 17:24
Pointers and references in C++: what happens with no pointers and no references.
#include <iostream>
using namespace std;
int main() {
int a = 1;
int b = a;
cout << "b gets the value that is inside a\n";
cout << "a=" << a << ", b=" << b << endl;
// prints: a=1, b=1
@DalyaG
DalyaG / hello_world.cpp
Created May 10, 2020 13:10
Hello World in C++
// Hello World
/* By Dalya G */
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
}
@DalyaG
DalyaG / stacked_bars_by_pet_color.py
Created May 9, 2020 20:38
Show stacked bars by pet color
ax = df_grouped_color.plot.bar(stacked=True, color=['brown', 'black'])
ax.set_xticklabels(labels=df_grouped_color.index, rotation=70, rotation_mode="anchor", ha="right")
ax.set_xlabel('')
ax.set_ylabel('n_pets');
@DalyaG
DalyaG / group_df_by_color.py
Created May 9, 2020 20:36
Group DataFrame by color
df['has_black'] = df['color'].str.contains("black")
df_grouped_color = (df.groupby(['weekday_num', 'weekday_name'])
['has_black']
.value_counts()
.unstack()
.reset_index(level=0, drop=True))
df_grouped_color
import matplotlib.pyplot as plt
df_sample_grouped = df_sample.groupby(['birthday']).size()
n_unique_dates = len(df_sample_grouped.index.unique())
fig = plt.figure(figsize=(n_unique_dates/5, n_unique_dates/10))
ax = df_sample_grouped.plot.bar(x="birthday", y="n_pets", color='blue')
ax.set_xticklabels(labels=df_sample_grouped.index, rotation=70, rotation_mode="anchor", ha="right");
ax.legend(labels=['n_pets']);
@DalyaG
DalyaG / birthday_paradox_plotted.py
Created May 9, 2020 20:33
Visualizing the Birthday Paradox
n_sample = 50
df_sample = df.sample(n=n_sample)
df_sample['birthday'] = (df_sample['date_of_birth']
.dt
.strftime('%m-%d'))
df_sample_grouped = (df_sample
.groupby(['birthday'])
.size()
.reset_index(name="n_pets"))
df_sample_grouped.plot.bar(x="birthday", y="n_pets", color='blue');
@DalyaG
DalyaG / plot_grouped_df.py
Created May 9, 2020 20:32
Plot DataFrame after grouping
df_grouped.plot.bar(x="weekday_name", y="n_pets", color='blue');
@DalyaG
DalyaG / group_df_by_weekday.py
Created May 9, 2020 20:31
Group DataFrame by weekday
df['weekday_num'] = pd.DatetimeIndex(df['date_of_birth']).weekday
df['weekday_name'] = pd.DatetimeIndex(df['date_of_birth']).weekday_name
df_grouped = (df.groupby(['weekday_num', 'weekday_name'])
.size()
.reset_index(name="n_pets"))
df_grouped
@DalyaG
DalyaG / read_shelter_data_to_data_frame.py
Created May 9, 2020 20:30
Read Shelter data to DataFrame
import pandas as pd
filename = "aac_shelter_cat_outcome_eng.csv"
df = pd.read_csv(filename,
usecols=['name', 'date_of_birth', 'color'],
parse_dates=['date_of_birth'])
df.head()