"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

October 10, 2016

Day #36 - Pandas Dataframe Learning's

import numpy as np
import pandas as pd
#Tip #1
#Data frame to matrix
df = pd.DataFrame({'A':[1,2,3,4],'B':[5,6,7,8],'C':[5,6,7,8]})
print(df)
#Tip #2
#Standardize value in columns
df["A"] = (df["A"]-df["A"].mean())/np.std(df["A"])
#Tip #3
#Dynamically stardardize except last column
for col in df.columns[:-1]:
df[col] = (df[col] - df[col].mean())/ (np.std(df[col]))
print(df)
features = list(df.columns[:-1])
#Tip #4 - Replance na values
df = df.fillna(-9999)
#Tip #5
#Dynamically add columns
for i in range(0,2):
colname1 = str(5+i)
col1 = i
col2 = i+1
print('colname', colname1)
print('col1', col1)
print('col2', col2)
if(col2 < 2):
df[colname1] = df[features[col1]]*df[features[col2]]
print('newly added columns')
print(df)
view raw pandasbasics.py hosted with ❤ by GitHub
Happy Learning!!!

No comments: