In [177]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import random as rnd
%matplotlib inline
In [4]:
np.arange(0,10, 2)
Out[4]:
In [5]:
np.linspace(10,23,6)
Out[5]:
In [23]:
rnd.seed(121)
rnd.random()
Out[23]:
In [51]:
l1 = [1,2,3,4]
a = np.array(l1)
a = a.reshape(2,2)
print(a)
print(a[:,0])
In [54]:
a.dtype
Out[54]:
In [64]:
np.transpose(np.ones(3).reshape(1,3)).dtype
Out[64]:
In [66]:
np.random.randn(2)
Out[66]:
In [67]:
np.random.uniform(0,1,5)
Out[67]:
In [72]:
plt.plot(np.random.uniform(0,1,5))
Out[72]:
In [85]:
countries = ['aus', 'Ind', 'pak', 'SL', 'Eng']
happiness_Index = ['8','4','4','6','8.5']
df = pd.DataFrame(happiness_Index, index = countries, columns = ['happiness_Index'])
df
df1 = pd.DataFrame(np.random.randn(5,5), index = "0,1,2,3,4".split(","), columns="a b c d e".split(" "))
df1
Out[85]:
In [96]:
df1.loc[['0']]
Out[96]:
In [99]:
df1.loc[['1','2'],['b','c']]
Out[99]:
In [108]:
df1.loc[:,['b','c']]
Out[108]:
In [109]:
df1 > 0
Out[109]:
In [111]:
df1[df1 > 0][['a','e']]
Out[111]:
In [112]:
df1['d'] > 0
Out[112]:
In [114]:
df1[df1['d'] > 0]
Out[114]:
In [136]:
newind = 'a0 a1 a2 a3 a4'.split()
In [137]:
df1['new_index'] = newind
In [138]:
df1
Out[138]:
In [139]:
df1.reset_index()
Out[139]:
In [141]:
df1.set_index('new_index')
In [144]:
from collections import defaultdict
In [146]:
d = defaultdict(list)
d['a'].append(100)
d['a'].append(101)
d['a'].append(102)
d['b'].append(200)
d['b'].append(201)
In [151]:
d['b'].append(np.nan)
d
Out[151]:
In [154]:
df2 = pd.DataFrame(d)
df2
Out[154]:
In [155]:
df2['Countries'] = 'aus ind eng'.split()
df2
Out[155]:
In [156]:
byCountry = df2.groupby('Countries')
In [162]:
df3 = pd.DataFrame(np.random.randn(3,3), index = 'a b c'.split(), columns = 'x y z'.split())
df3
Out[162]:
In [171]:
x = np.linspace(0,10,5)
y2 = y**2
y3 = x**3
print(y2)
print(y3)
In [167]:
plt.plot(x,y3)
Out[167]:
In [168]:
plt.subplot(1,2,1)
plt.plot(x,y2,'r--')
plt.subplot(1,2,2)
plt.plot(x,y3,'g--')
Out[168]:
In [190]:
fig = plt.figure(figsize=(2,1), dpi=50)
axes = fig.add_axes([0,0,2,2])
axes.plot(x, y2, 'b')
Out[190]:
In [197]:
fig, axes = plt.subplots(nrows=1, ncols=2)
In [204]:
for ax in axes:
ax.plot(x, y, 'b')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('title')
fig
plt.tight_layout()
lets look at seaborn library
In [205]:
import seaborn as sns
In [206]:
tips = sns.load_dataset('tips')
tips.head()
Out[206]:
In [207]:
sns.violinplot(x="day", y="total_bill", data=tips,hue='sex',palette='rainbow')
Out[207]:
seen
ReplyDelete