% matplotlib inline
import pandas as pd
import numpy as np
import matplotlib. pyplot as plt
dates = pd. date_range( "20160301" , periods= 6 )
df = pd. DataFrame( np. random. randn( 6 , 4 ) , index= dates, columns= list ( "ABCD" ) )
df
A B C D 2016-03-01 -0.248598 0.048589 0.493832 0.379403 2016-03-02 -0.677500 -1.319923 -1.730255 0.011082 2016-03-03 1.041719 -1.248769 0.287455 -0.243821 2016-03-04 -1.351725 -0.948413 -0.168241 -0.206230 2016-03-05 -0.712360 -0.676127 -0.218639 -1.713820 2016-03-06 -2.107682 -1.155953 -0.982534 -1.005173
df1 = df. reindex( index= dates[ 0 : 4 ] , columns= list ( df. columns) + [ "E" ] )
df1
A B C D E 2016-03-01 -0.248598 0.048589 0.493832 0.379403 NaN 2016-03-02 -0.677500 -1.319923 -1.730255 0.011082 NaN 2016-03-03 1.041719 -1.248769 0.287455 -0.243821 NaN 2016-03-04 -1.351725 -0.948413 -0.168241 -0.206230 NaN
df1. loc[ dates[ 1 : 3 ] , "E" ] = 2
df1
A B C D E 2016-03-01 -0.248598 0.048589 0.493832 0.379403 NaN 2016-03-02 -0.677500 -1.319923 -1.730255 0.011082 2.0 2016-03-03 1.041719 -1.248769 0.287455 -0.243821 2.0 2016-03-04 -1.351725 -0.948413 -0.168241 -0.206230 NaN
df1. dropna( )
A B C D E 2016-03-02 -0.677500 -1.319923 -1.730255 0.011082 2.0 2016-03-03 1.041719 -1.248769 0.287455 -0.243821 2.0
df1. fillna( value= 5 )
A B C D E 2016-03-01 -0.248598 0.048589 0.493832 0.379403 5.0 2016-03-02 -0.677500 -1.319923 -1.730255 0.011082 2.0 2016-03-03 1.041719 -1.248769 0.287455 -0.243821 2.0 2016-03-04 -1.351725 -0.948413 -0.168241 -0.206230 5.0
pd. isnull( df1) . any ( ) . any ( )
True
df1. mean( )
A -0.309026
B -0.867129
C -0.279302
D -0.014891
E 2.000000
dtype: float64
df1. mean( axis= 1 )
2016-03-01 0.168306
2016-03-02 -0.343319
2016-03-03 0.367317
2016-03-04 -0.668652
Freq: D, dtype: float64
df1. cumsum( )
A B C D E 2016-03-01 -0.248598 0.048589 0.493832 0.379403 NaN 2016-03-02 -0.926098 -1.271334 -1.236423 0.390485 2.0 2016-03-03 0.115621 -2.520104 -0.948968 0.146664 4.0 2016-03-04 -1.236104 -3.468517 -1.117209 -0.059566 NaN
s = pd. Series( [ 1 , 3 , 5 , np. nan, 6 , 8 ] , index= dates) . shift( 2 )
s
2016-03-01 NaN
2016-03-02 NaN
2016-03-03 1.0
2016-03-04 3.0
2016-03-05 5.0
2016-03-06 NaN
Freq: D, dtype: float64
df
A B C D 2016-03-01 -0.248598 0.048589 0.493832 0.379403 2016-03-02 -0.677500 -1.319923 -1.730255 0.011082 2016-03-03 1.041719 -1.248769 0.287455 -0.243821 2016-03-04 -1.351725 -0.948413 -0.168241 -0.206230 2016-03-05 -0.712360 -0.676127 -0.218639 -1.713820 2016-03-06 -2.107682 -1.155953 -0.982534 -1.005173
df. sub( s, axis= "index" )
A B C D 2016-03-01 NaN NaN NaN NaN 2016-03-02 NaN NaN NaN NaN 2016-03-03 0.041719 -2.248769 -0.712545 -1.243821 2016-03-04 -4.351725 -3.948413 -3.168241 -3.206230 2016-03-05 -5.712360 -5.676127 -5.218639 -6.713820 2016-03-06 NaN NaN NaN NaN
df
A B C D 2016-03-01 -0.248598 0.048589 0.493832 0.379403 2016-03-02 -0.677500 -1.319923 -1.730255 0.011082 2016-03-03 1.041719 -1.248769 0.287455 -0.243821 2016-03-04 -1.351725 -0.948413 -0.168241 -0.206230 2016-03-05 -0.712360 -0.676127 -0.218639 -1.713820 2016-03-06 -2.107682 -1.155953 -0.982534 -1.005173
df. apply ( np. cumsum)
A B C D 2016-03-01 -0.248598 0.048589 0.493832 0.379403 2016-03-02 -0.926098 -1.271334 -1.236423 0.390485 2016-03-03 0.115621 -2.520104 -0.948968 0.146664 2016-03-04 -1.236104 -3.468517 -1.117209 -0.059566 2016-03-05 -1.948464 -4.144644 -1.335848 -1.773385 2016-03-06 -4.056147 -5.300596 -2.318382 -2.778558
df. apply ( lambda x : x. max ( ) - x. min ( ) )
A 3.149402
B 1.368512
C 2.224087
D 2.093223
dtype: float64
def _sum ( x) :
print ( type ( x) )
return x. sum ( )
df. apply ( _sum)
<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
A -4.056147
B -5.300596
C -2.318382
D -2.778558
dtype: float64
s = pd. Series( np. random. randint( 10 , 20 , size= 20 ) )
s
0 14
1 19
2 15
3 16
4 15
5 13
6 17
7 11
8 16
9 13
10 14
11 12
12 11
13 12
14 14
15 11
16 14
17 13
18 15
19 18
dtype: int32
s. value_counts( )
14 4
15 3
13 3
11 3
16 2
12 2
19 1
18 1
17 1
dtype: int64
s. mode( )
0 14
dtype: int32
df = pd. DataFrame( np. random. randn( 10 , 4 ) , columns= list ( "ABCD" ) )
df
A B C D 0 -0.258511 0.839909 -1.242957 -1.542059 1 -0.045973 1.434317 -0.701617 -1.780006 2 -0.188364 -0.187067 0.725562 -0.706046 3 0.999175 0.749767 0.834201 -0.545250 4 0.167320 0.444925 -1.100763 0.588002 5 0.135248 -1.586037 -0.638731 -0.240570 6 -0.457926 -1.206899 -1.017282 -1.336446 7 0.436207 2.192452 0.845839 -1.458328 8 1.485075 0.189602 -0.232063 -0.873814 9 1.082126 1.491539 0.290744 0.336791
df. iloc[ : 3 ]
A B C D 0 -0.258511 0.839909 -1.242957 -1.542059 1 -0.045973 1.434317 -0.701617 -1.780006 2 -0.188364 -0.187067 0.725562 -0.706046
df. iloc[ 3 : 7 ]
A B C D 3 0.999175 0.749767 0.834201 -0.545250 4 0.167320 0.444925 -1.100763 0.588002 5 0.135248 -1.586037 -0.638731 -0.240570 6 -0.457926 -1.206899 -1.017282 -1.336446
df. iloc[ 7 : ]
A B C D 7 0.436207 2.192452 0.845839 -1.458328 8 1.485075 0.189602 -0.232063 -0.873814 9 1.082126 1.491539 0.290744 0.336791
df1 = pd. concat( [ df. iloc[ : 3 ] , df. iloc[ 3 : 7 ] , df. iloc[ 7 : ] ] )
df1
A B C D 0 -0.258511 0.839909 -1.242957 -1.542059 1 -0.045973 1.434317 -0.701617 -1.780006 2 -0.188364 -0.187067 0.725562 -0.706046 3 0.999175 0.749767 0.834201 -0.545250 4 0.167320 0.444925 -1.100763 0.588002 5 0.135248 -1.586037 -0.638731 -0.240570 6 -0.457926 -1.206899 -1.017282 -1.336446 7 0.436207 2.192452 0.845839 -1.458328 8 1.485075 0.189602 -0.232063 -0.873814 9 1.082126 1.491539 0.290744 0.336791
( df== df1) . all ( ) . all ( )
True
left = pd. DataFrame( { "key" : [ "foo" , "foo" ] , "lval" : [ 1 , 2 ] } )
right = pd. DataFrame( { "key" : [ "foo" , "foo" ] , "rval" : [ 4 , 5 ] } )
left
right
pd. merge( left, right, on= "key" )
key lval rval 0 foo 1 4 1 foo 1 5 2 foo 2 4 3 foo 2 5
s = pd. Series( np. random. randint( 1 , 5 , size= 5 ) , index= list ( "ABCDE" ) )
s
A 1
B 1
C 4
D 4
E 2
dtype: int32
df. append( s, ignore_index= True )
A B C D E 0 -0.258511 0.839909 -1.242957 -1.542059 NaN 1 -0.045973 1.434317 -0.701617 -1.780006 NaN 2 -0.188364 -0.187067 0.725562 -0.706046 NaN 3 0.999175 0.749767 0.834201 -0.545250 NaN 4 0.167320 0.444925 -1.100763 0.588002 NaN 5 0.135248 -1.586037 -0.638731 -0.240570 NaN 6 -0.457926 -1.206899 -1.017282 -1.336446 NaN 7 0.436207 2.192452 0.845839 -1.458328 NaN 8 1.485075 0.189602 -0.232063 -0.873814 NaN 9 1.082126 1.491539 0.290744 0.336791 NaN 10 1.000000 1.000000 4.000000 4.000000 2.0
df = pd. DataFrame( { "A" : [ "foo" , "bar" , "foo" , "bar" ,
"foo" , "bar" , "foo" , "foo" ] ,
"B" : [ "one" , "one" , "two" , "three" ,
"two" , "two" , "one" , "three" ] ,
"C" : np. random. randn( 8 ) ,
"D" : np. random. randn( 8 ) } )
df
A B C D 0 foo one -0.883521 1.259531 1 bar one -0.353476 0.054437 2 foo two -0.257092 0.574759 3 bar three 0.678201 -1.091763 4 foo two -1.625564 0.475802 5 bar two 1.418208 -0.240999 6 foo one 0.535367 -0.925612 7 foo three -0.401202 0.009708
df. groupby( "A" ) . sum ( )
C D A bar 1.742932 -1.278326 foo -2.632012 1.394188
df. groupby( [ "A" , "B" ] ) . sum ( )
C D A B bar one -0.353476 0.054437 three 0.678201 -1.091763 two 1.418208 -0.240999 foo one -0.348154 0.333919 three -0.401202 0.009708 two -1.882656 1.050561
df. groupby( [ "B" , "A" ] ) . sum ( )
C D B A one bar -0.353476 0.054437 foo -0.348154 0.333919 three bar 0.678201 -1.091763 foo -0.401202 0.009708 two bar 1.418208 -0.240999 foo -1.882656 1.050561