Python Pandas
Python Pandas
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mathplotlib import style
style.use('ggplot')
web_stats = {'Day':[1,2,3,4,5,6],
'Visitors': [4,4,4,5,5,6],
'Bounce_Rate':[4,4,4,5,5,6]}
df = pd.DataFrame(web_stats)
#print(df)
#print(df.head())
#print(df.tail())
#print(df.tail(2))
#Convert to List
print(df.Visistors.tolist())
#Convert to Array
print(np.array(df[['Visitors', 'Bounce_Rate']]))
###################
################### IO (Converter to anything)
import pandas as pd
df = pd.read_csv('FileName.csv')
df.set_index('Date', inplace=True)
print(df.head())
df.to_csv('NewFile.csv')
df.read_csv('NewFile.csv', index_col=0)
df.to_csv('NewFile2.csv', header=False)
#Define Headers, if not defined
df.read_csv('NewFile.csv', names=['Date','Austin_HPI'],index_col=0)
print(df.head())
#To HTML
df.to_html('eample.html')
#Column Rename
df.rename(columns={'Autin_HPI':'7th Digit of Autin'}, inplace=True)
######### Quandl
#########
pip install quandl
import Quandl
import pandas as pd
api_key = open('quadlkey.text','r').read()
df = Quandl.get('FMAC/HPI_AK', authtoken=api_key)
print(df.head())
#Load from li
# Read from List,
fiddy_states = pd.read_html('http://list of us')
# this is list
print(fiddy_states)
import pandas as pd
df1 = pd.DataFrame({'HPI':[80,85,88,85],
'Int_rate':[2, 3, 2, 2],
'US_GDP_Thousands':[50, 55, 65, 55]},
index = [2001, 2002, 2003, 2004])
df2 = pd.DataFrame({'HPI':[80,85,88,85],
'Int_rate':[2, 3, 2, 2],
'US_GDP_Thousands':[50, 55, 65, 55]},
index = [2005, 2006, 2007, 2008])
df3 = pd.DataFrame({'HPI':[80,85,88,85],
'Unemployment':[7, 8, 9, 6],
'Low_tier_HPI':[50, 52, 50, 53]},
index = [2001, 2002, 2003, 2004])
joined = df1.join(df3)
print(joined)
#Merged
print(pd.merge(df1,df2,on = 'HPI', how = 'outer'))
##### Part - 7 -
################################