This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def remove(str_): | |
return re.sub(pattern, '', str_) | |
df.column.apply(remove) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For Vertical | |
plt.vlines(x= df.index, ymin= df.mean(), ymax= df.values, alpha=0.4, linewidth=5) | |
# For Horizontal | |
plt.hlines(y= df.index, xmin= df.mean(), xmax= df.values, alpha=0.4, linewidth=5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plt.savefig('myfile.png', bbox_inches="tight") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Examples can be found in Pandas Book (Notebooks) 3 Section > 2. Combining, Merging | |
# This is very manual (but a kind of hack) | |
df.stack(level= [0, 1]).unstack(level= [1, 2, 3]) | |
# Desired level and their order ↑ (Both stack / unstack required) | |
# Real Way - efficient | |
df.unstack().swaplevel(2, 0, axis= 1).sort_index(level= 0, aixs= 1) | |
# This is the ↑ person. ↑ This sorting is optional but required for cleaner dataset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''There can be a situation while cleaning the data | |
you might want ot replace one dataframe's value on | |
certain indices with other df's values (usually cl- | |
eared ones) based on the index - This MIGHT help. | |
2 Solutions.''' | |
# 1. Great - Almost all time will work | |
df.col_to_change.update(df.col_new_value) | |
' As both are Series - this will work! ' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# AMAZING! | |
df.columns.get_level_values(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Example data | |
Name Genre | |
0 TENET Action|Thriller | |
1 MEMENTO Crime|Thriller|Action | |
2 AVENGERS Children's | |
''' | |
# SPOILER ALERT: This method is the UNDERLYING method. Just use - df.Genre.str.get_dummies("|") for the same result (more on this later) | |
# Step 1: Get the unique genre |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Wow way | |
df[(np.abs(df) > 2).any(1)].style.applymap(lambda x: "background: yellow" if np.abs(x) > 2 else "") | |
# ↑ Here applymap is used which | |
# is eqivalent to the code below | |
# Verbose, but still more control way | |
df[(np.abs(df) > 2).any(1)].style.apply(lambda x: ["background: yellow" if np.abs(value) > 2 else "" for value in x]) | |
# ↑ Only apply is used, so needed to itereate over all elements in this ↑ list comp way | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
plt.xkcd() # <-- This does the job | |
plt.figure() | |
plt.plot(np.linspace(0.7,1.42,100),[0.7]*100) | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DF.dateCol.apply(lambda x: x.replace(year= x.year + 2)) |