Skip to content

Instantly share code, notes, and snippets.

View pygaurav's full-sized avatar
🎯
Focusing

Gaurav Bhardwaj pygaurav

🎯
Focusing
View GitHub Profile
@pygaurav
pygaurav / decorators.py
Created December 18, 2019 15:49
Decorators in Python
# Simple Decorator Code Starts
# a = {"isAdmin":True}
# def custom_decorators(func):
# def inner_function():
# if a["isAdmin"]:
# return func()
# return inner_function
@pygaurav
pygaurav / exporting_files_pandas.py
Created June 29, 2019 08:16
Exporting Files Pandas
import pandas as pd
import numpy as np
dataframe1 = pd.DataFrame({"Class_ID":[1,2,3,4],
"Name":["Adam","Jack","Ram","Krishna"]})
dataframe2 = pd.DataFrame({"Class_ID":[2,3,4,10,11],
"Marks":[20,30,40,60,70]})
@pygaurav
pygaurav / inner_merge_pandas.py
Created June 29, 2019 08:04
Inner Merge Pandas
import pandas as pd
import numpy as np
dataframe1 = pd.DataFrame({"Class_ID":[1,2,3,4],
"Name":["Adam","Jack","Ram","Krishna"]})
dataframe2 = pd.DataFrame({"Class_ID":[2,3,4,10,11],
"Marks":[20,30,40,60,70]})
@pygaurav
pygaurav / outer_merge_pandas.py
Created June 29, 2019 07:47
Outer Merge Pandas
import pandas as pd
import numpy as np
dataframe1 = pd.DataFrame({"Class_ID":[1,2,3,4],
"Name":["Adam","Jack","Ram","Krishna"]})
dataframe2 = pd.DataFrame({"Class_ID":[2,3,4,10,11],
"Marks":[20,30,40,60,70]})
@pygaurav
pygaurav / right_merge_pandas.py
Created June 29, 2019 07:38
Right Merge Pandas
import pandas as pd
import numpy as np
dataframe1 = pd.DataFrame({"Class_ID":[1,2,3,4],
"Name":["Adam","Jack","Ram","Krishna"]})
dataframe2 = pd.DataFrame({"Class_ID":[2,3,4,10,11],
"Marks":[20,30,40,60,70]})
@pygaurav
pygaurav / left_merge_pandas.py
Created June 29, 2019 07:09
Left Merge Pandas
import pandas as pd
import numpy as np
dataframe1 = pd.DataFrame({"Class_ID":[1,2,3,4],
"Name":["Adam","Jack","Ram","Krishna"]})
dataframe2 = pd.DataFrame({"Class_ID":[2,3,4,10,11],
"Marks":[20,30,40,60,70]})
@pygaurav
pygaurav / pandas_group_by_custom.py
Created June 28, 2019 16:08
Pandas group by custom function
import pandas as pd
import numpy as np
#Get the mean of the group less than 5
def get_mean_group_lessthan_five(arr):
return arr[(arr<5)].mean()
column_names = ["sepal length","sepal width","petal length","petal width","Type of flower"]
df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",
names=column_names)
@pygaurav
pygaurav / pandas_group_by.py
Created June 28, 2019 15:52
Pandas Group By basic
import pandas as pd
import numpy as np
column_names = ["sepal length","sepal width","petal length","petal width","Type of flower"]
df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",
names=column_names)
#Check the mean sepal length, sepal width, petal length, petal width on the
#basis of type of flower
abc = df.groupby('Type of flower').mean()
print(abc)
@pygaurav
pygaurav / custom_aggregate_function.py
Created June 27, 2019 20:11
Custom Aggregate Function
import pandas as pd
import numpy as np
# Method for Ranking the Category
# if the mean is less than 4 it will return 1 category else 2
def category_name(arr):
mean = arr.mean()
if mean>4:
return 1
else:
@pygaurav
pygaurav / aggregate_data_pandas.py
Created June 27, 2019 19:33
aggregate_data_pandas
import pandas as pd
column_names = ["sepal length","sepal width","petal length","petal width","Type of flower"]
df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",
names=column_names)
abc = df.agg(["mean","sum","std"])
print(abc)