Skip to content

Instantly share code, notes, and snippets.

View AayushSameerShah's full-sized avatar
🤨
Hmm...

Aayush Shah AayushSameerShah

🤨
Hmm...
View GitHub Profile
@AayushSameerShah
AayushSameerShah / safe_divide_numpy.py
Created September 2, 2021 11:25
This will successfully divide by zero and will return zero
a = [1, 2, 3]
b = [0, 1, 0] # Arr with Zeros
c = np.divide(a, b,
out=np.zeros_like(a), where=b!=0)
'''Simply replace a and b with your variables'''
@AayushSameerShah
AayushSameerShah / legend_outside.py
Created August 30, 2021 07:18
Just add one line and the legend will be thrown away!
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
@AayushSameerShah
AayushSameerShah / generate_color_plotly.py
Created August 12, 2021 05:55
This will generate the colors for plotly (or for others as well)
# Number of elements (total colors to generate in short)
N = 30
c = ['hsl('+str(h)+',50%'+',50%)' for h in np.linspace(0, 360, N)]
import pandas as pd
import io
import requests
url="https://raw.githubusercontent.com/cs109/2014_data/master/countries.csv"
s=requests.get(url).content
c=pd.read_csv(io.StringIO(s.decode('utf-8')))
# This can be made SUPER easily in tableau
# visit this link
https://www.youtube.com/watch?v=08xh-vvSUqg
@AayushSameerShah
AayushSameerShah / categorize_multiple3.py
Created July 2, 2021 14:03
Get the most repeated values - LIKE GENERES of movies
# THIS IS AGAIN GONNA BE AMAZING!
'''
Sometimes we have a LOT - means a million lines of data and making the str.get_dummies("|") will
cause the Insufficient Memory Error.
So instead of just splitting them and making zeros and ones... (well that is good if we want to
keep track the word per movies, but if overall - then that is not good practice)
'''
# NEW THING!
@AayushSameerShah
AayushSameerShah / Sort_in_group_multiple.py
Created June 21, 2021 12:13
When you have multiple columns to groupby and your fingers are tempted to use all column names at once in groupby syntax, don't do that. Do this instead.
# Define a function in which the group will be passed
def get_nlargest(group):
# That group's group will be created (not complex)
group = group.groupby('key2').value_col.sum()
return group.nlargest(5)
# X Instead of doing this ↓
df.groupby(['key1', 'key2').apply(lamda group: group...)
@AayushSameerShah
AayushSameerShah / make_deck.py
Created June 10, 2021 13:56
This is how to quickly make a deck of 52 cards
series = pd.Series([], dtype= int)
for suite in ['H', 'D', 'S', 'C']:
for value, number in enumerate(['A'] + list(range(1, 11)) + ['J', 'Q', 'K']):
series = series.add(pd.Series({str(number) + suite: value + 1}), fill_value= 0)
@AayushSameerShah
AayushSameerShah / groupby_rows.py
Created June 9, 2021 14:49
Here, the simplest form of groupping by rows is present
# Simple and easy
df.gropby(df.index // n_rows).mean()
plt.rcParams['axes.spines.left'] = False
plt.rcParams['axes.spines.right'] = False
plt.rcParams['axes.spines.top'] = False
plt.rcParams['axes.spines.bottom'] = False