Pands基礎使用まとめ

4873 ワード

import pandas as pd
import numpy as np

#     Series:eries           ,                  ,           。
s = pd.Series([1,3,5,np.nan,6,8])
#print(s)

#    DataFrame:DataFrame            ,            。
#        
dates = pd.date_range('20140101',periods=6)
#  Dataframe,  index      ,columns    
df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))
#print(df)
#           DataFrame
df2 = pd.DataFrame({ 'A' : 1.,
                     'B' : pd.Timestamp('20130102'),
                     'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
                     'D' : np.array([3] * 4,dtype='int32'),
                     'E' : pd.Categorical(["test","train","test","train"]),
                     'F' : 'foo' })
#print(df2)
#          
#print(df2.dtypes)



#    
#       
#print(df.head())
#       
#print(df.tail(3))
#    
#print(df.index)
#   
#print(df.columns)
#    numpy  
#print(df.values)
#DataFrame           
#print(df.describe())
#  
#print(df.T)
#            
#print(df.sort_index(axis=1,ascending=False))


#  /  
#       ,   Serires,  df.A     。
#print(df['A'])
#    
#print(df[0:3])
#    
#print(df['20140102':'20140104'])
#      
#print(df.loc[dates[0]])
#              
#print(df.loc[:,['A','B']])
#          
#print(df.loc[dates[0],'A'])
#        
#print(df.iloc[3])
#  
#print(df.iloc[3:5,0:2])
#    
#print(df.iloc[[1,2,4],[0,2]])
#          
#print(df.iloc[1,1])
#    
#                
#print(df[df.A>0])
#          
#print(df[df>0])
#   isin()       
df3 = df.copy()
df3['E'] = ['one','one','two','three','four','three']
#print(df3)
#print(df3[df3['E'].isin(['two','four'])])

#  
#    ,      
s1=pd.Series([1,2,3,4,5,6],index=pd.date_range('20140102',periods=6))
#print(s1)
df['F']=s1
df.at[dates[0],'A']=0
df.iat[0,1]=0
df.loc[:,'D']=np.array([5]*len(df))
#print(df)
#      
df4=df.copy()
df4[df4 >0] = -df4
#print(df4)

#   
#  pandas     np.nan        。
df5 = df.reindex(index=dates[0:4], columns=list(df.columns) + ['E'])
df5.loc[dates[0]:dates[1],'E'] = 1
#print(df5)
#           
#print(df5.dropna(how='any'))
#     
#print(df5.fillna(value=5))
#          
#print(pd.isnull(df5))


#    
#  
#    
#print(df.mean())
#         
#print(df.mean(1))
#      pandas         ,        ,shift        
s = pd.Series([1,3,5,np.nan,6,8], index=dates).shift(2)
#print(s)
#       pandas         :
#print(df.sub(s, axis='index'))
#      numpy   :
#print(df.apply(np.cumsum)) #  
#       
#print(df.apply(lambda x: x.max() - x.min()))
#           
s = pd.Series(np.random.randint(0, 7, size=10))
#print(s)
#print(s.value_counts())

#    
s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat'])
#print(s.str.lower())

#  
#   concat()    pandas   :
df = pd.DataFrame(np.random.randn(10, 4))
#print(df)
piects=[df[:3],df[3:7],df[7:]]
#print(pd.concat(piects))

#Join
left = pd.DataFrame({'key': ['foo', 'foo'], 'lval': [1, 2]})
right = pd.DataFrame({'key': ['foo', 'foo'], 'rval': [4, 5]})
#print(pd.merge(left, right, on='key'))

#  
df = pd.DataFrame(np.random.randn(8, 4), columns=['A','B','C','D'])
#print(df)
s=df.iloc[3]
#print(df.append(s,ignore_index=True))


#                         
#          
#         
#             
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)})
#print(df)
#         ,       bar    foo  ,      。
#print(df.groupby('A').sum())
#                :
#print(df.groupby(['A','B']).sum())

#     
df = pd.DataFrame({'A' : ['one', 'one', 'two', 'three'] * 3,
                   'B' : ['A', 'B', 'C'] * 4,
                   'C' : ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 2,
                   'D' : np.random.randn(12),
                   'E' : np.random.randn(12)})
#       
print(pd.pivot_table(df, values='D', index=['A', 'B'], columns=['C']))