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 pandas as pd |
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
data = pd.read_csv(“Salary.csv”) | |
print(data.head()) |
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 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()) |
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
# 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 |
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 library | |
import pandas as pd | |
import numpy as np |
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
#creating a dataframe | |
df=pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=['a', 'b', 'c']) |
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
#applying the transform function | |
df.transform(func = lambda x : x * 10) | |
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 pandas as pd | |
df=pd.read_csv(“purchase.csv”) #Can be any csv of your choice. |
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.groupby('User_ID')["Purchase"].mean() |
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
mean_purchase =df.groupby('User_ID')["Purchase"].mean().rename("User_mean").reset_index() | |
df_1 = df.merge(mean_purchase) |
OlderNewer