Skip to content

Instantly share code, notes, and snippets.

View One1.py
import numpy as np
import pandas as pd
View One2.py
data = pd.read_csv(“Salary.csv”)
print(data.head())
View One3.py
# Import label encoder
from sklearn import preprocessing
# label_encoder object knows how to understand word labels.
label_encoder = preprocessing.LabelEncoder()
# Encode labels in column 'Country'.
data['Country']= label_encoder.fit_transform(data[‘Country'])
print(data.head())
View One4.py
# importing one hot encoder
from sklearn from sklearn.preprocessing import OneHotEncoder
# creating one hot encoder object
onehotencoder = OneHotEncoder()
#reshape the 1-D country array to 2-D as fit_transform expects 2-D and finally fit the object
X = onehotencoder.fit_transform(data.Country.values.reshape(-1,1)).toarray()
#To add this back into the original dataframe
dfOneHot = pd.DataFrame(X, columns = ["Country_"+str(int(i)) for i in range(data.shape[1])])
df = pd.concat([data, dfOneHot], axis=1)
#droping the country column
View T1.py
#import library
import pandas as pd
import numpy as np
View T2.py
#creating a dataframe
df=pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=['a', 'b', 'c'])
View T3.py
#applying the transform function
df.transform(func = lambda x : x * 10)
View T4.py
import pandas as pd
df=pd.read_csv(“purchase.csv”)  #Can be any csv of your choice.
View T5.py
df.groupby('User_ID')["Purchase"].mean()
View T6.py
mean_purchase =df.groupby('User_ID')["Purchase"].mean().rename("User_mean").reset_index()
df_1 = df.merge(mean_purchase)