Open
Anaconda Navigator
Launch
Sypder
If you have not seen the Pandas
PROGRAM
1
import pandas as pd
import numpy as np
s1 = pd.Series([1,2,3,4,5,4])
s2 = pd.Series(np.random.randn(10))
print (s1.pct_change())
d= pd.DataFrame(np.random.randn(5, 5),columns=['a', 'b', 'c', 'd', 'e'])
print('\n Percent change \n')
print (d.pct_change()) #percentage change
print('\n covariance 1 \n')
print (s1.cov(s2)) #covariance
print('\n covariance 2\n')
print (d['a'].cov(d['b']))
print (d.cov())
print('\n correlation\n')
print (d['a'].corr(d['b']))
print(d.corr())
print('\n Data Ranking \n')
d['d'] = d['b'] # so there's a tie
print (d.rank())
OUTPUT
runfile('C:/Users/dell/temp2.py', wdir='C:/Users/dell')
0 NaN
1 1.000000
2 0.500000
3 0.333333
4 0.250000
5 -0.200000
dtype: float64
Percent change
a b c d e
0 NaN NaN
NaN NaN
NaN
1 -2.051009 -0.060892 -1.433328 -0.540440 -1.233141
2 -3.766884 -1.276649 -0.819115 -5.498268 -6.156215
3 -0.312564 -2.389023 0.354732 -0.625432 -0.337789
4 -2.214880 -2.328694 -1.138566 -0.603732 -0.876367
covariance 1
0.9431969011296396
covariance 2
-0.08364434111414272
a
b c
d e
a 0.523685 -0.083644 -0.159114 -0.530667 -0.413056
b -0.083644 3.155017 0.717062 -1.827976 -0.129688
c -0.159114 0.717062 1.767122 -0.847141 0.455338
d -0.530667 -1.827976 -0.847141 1.937669 0.398311
e -0.413056 -0.129688 0.455338 0.398311 0.426769
correlation
-0.06507296323532948
a
b c
d e
a 1.000000 -0.065073 -0.165402 -0.526801 -0.873731
b -0.065073 1.000000 0.303684 -0.739315 -0.111764
c -0.165402 0.303684 1.000000 -0.457807 0.524330
d -0.526801 -0.739315 -0.457807 1.000000 0.438012
e -0.873731 -0.111764 0.524330 0.438012 1.000000
Data Ranking
a b c d e
0 3.0 1.0 1.0 1.0 2.0
1 2.0 2.0 5.0 2.0 5.0
2 5.0 4.0 3.0 4.0 1.0
3 4.0 3.0 4.0 3.0 3.0
4 1.0 5.0 2.0 5.0 4.0
More tutorials to come soon!