pandas 의 DataFrame

32590 단어 ML
DataFrame 대상 의 생 성, 수정, 통합

import pandas as pd
import numpy as np
     
     
     
     
  • 1
  • 2
  • 3

创建DataFrame对象

#   DataFrame  
df = pd.DataFrame([1, 2, 3, 4, 5], columns=['cols'], index=['a','b','c','d','e'])
print df
     
     
     
     
  • 1
  • 2
  • 3
   cols
a     1
b     2
c     3
d     4
e     5

     
     
     
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
df2 = pd.DataFrame([[1, 2, 3],[4, 5, 6]], columns=['col1','col2','col3'], index=['a','b'])
print df2
     
     
     
     
  • 1
  • 2
   col1  col2  col3
a     1     2     3
b     4     5     6

     
     
     
     
  • 1
  • 2
  • 3
  • 4
df3 = pd.DataFrame(np.array([[1,2],[3,4]]), columns=['col1','col2'], index=['a','b'])
print df3
     
     
     
     
  • 1
  • 2
   col1  col2
a     1     2
b     3     4

     
     
     
     
  • 1
  • 2
  • 3
  • 4
df4 = pd.DataFrame({'col1':[1,3],'col2':[2,4]},index=['a','b'])
print df4
     
     
     
     
  • 1
  • 2
   col1  col2
a     1     2
b     3     4

     
     
     
     
  • 1
  • 2
  • 3
  • 4
  DataFrame          ,     ,          
     
     
     
     
  • 1

基本操作

# DataFrame       
df2.index
     
     
     
     
  • 1
  • 2
Index([u'a', u'b'], dtype='object')

     
     
     
     
  • 1
  • 2
df2.columns
     
     
     
     
  • 1
Index([u'col1', u'col2', u'col3'], dtype='object')

     
     
     
     
  • 1
  • 2
#         
df2.loc['a']   
#    a      
# df2.iloc[0]         ,        ,             
     
     
     
     
  • 1
  • 2
  • 3
  • 4
col1    1
col2    2
col3    3
Name: a, dtype: int64

     
     
     
     
  • 1
  • 2
  • 3
  • 4
  • 5
print df2.loc[['a','b']]    #       ,           
     
     
     
     
  • 1
   col1  col2  col3
a     1     2     3
b     4     5     6

     
     
     
     
  • 1
  • 2
  • 3
  • 4
print df.loc[df.index[1:3]]
     
     
     
     
  • 1
   cols
b     2
c     3

     
     
     
     
  • 1
  • 2
  • 3
  • 4
#      
print df2[['col1','col3']]
     
     
     
     
  • 1
  • 2
   col1  col3
a     1     3
b     4     6

     
     
     
     
  • 1
  • 2
  • 3
  • 4

计算

# DataFrame    
#           
print df2.sum()
     
     
     
     
  • 1
  • 2
  • 3
col1    5
col2    7
col3    9
dtype: int64

     
     
     
     
  • 1
  • 2
  • 3
  • 4
  • 5
#    
print df2.sum(1)
     
     
     
     
  • 1
  • 2
a     6
b    15
dtype: int64

     
     
     
     
  • 1
  • 2
  • 3
  • 4
#        2
print df2.apply(lambda x:x*2)
     
     
     
     
  • 1
  • 2
   col1  col2  col3
a     2     4     6
b     8    10    12

     
     
     
     
  • 1
  • 2
  • 3
  • 4
#         (  ndarray        )
print df2**2
     
     
     
     
  • 1
  • 2
   col1  col2  col3
a     1     4     9
b    16    25    36

     
     
     
     
  • 1
  • 2
  • 3
  • 4

列扩充

#  DataFrame       
df2['col4'] = ['cnn','rnn']
print df2
     
     
     
     
  • 1
  • 2
  • 3
   col1  col2  col3 col4
a     1     2     3  cnn
b     4     5     6  rnn

     
     
     
     
  • 1
  • 2
  • 3
  • 4
#          DataFrame         ,      
df2['col5'] = pd.DataFrame(['MachineLearning','DeepLearning'],index=['a','b'])
print df2
     
     
     
     
  • 1
  • 2
  • 3
   col1  col2  col3 col4             col5
a     1     2     3  cnn  MachineLearning
b     4     5     6  rnn     DeepLearning

     
     
     
     
  • 1
  • 2
  • 3
  • 4

行扩充

#      
print df2.append(pd.DataFrame({'col1':7,'col2':8,'col3':9,'col4':'rcnn','col5':'ReinforcementLearning'},index=['c']))
     
     
     
     
  • 1
  • 2
   col1  col2  col3  col4                   col5
a     1     2     3   cnn        MachineLearning
b     4     5     6   rnn           DeepLearning
c     7     8     9  rcnn  ReinforcementLearning

     
     
     
     
  • 1
  • 2
  • 3
  • 4
  • 5

注意!

#              ,  index   ,        
print df2.append({'col1':10,'col2':11,'col3':12,'col4':'frnn','col5':'DRL'},ignore_index=True)
     
     
     
     
  • 1
  • 2
   col1  col2  col3  col4             col5
0     1     2     3   cnn  MachineLearning
1     4     5     6   rnn     DeepLearning
2    10    11    12  frnn              DRL

     
     
     
     
  • 1
  • 2
  • 3
  • 4
  • 5
#       ,       ,df2  DataFrame  ,  
df2 = df2.append(pd.DataFrame({'col1':7,'col2':8,'col3':9,'col4':'rcnn','col5':'ReinforcementLearning'},index=['c']))
print df2
     
     
     
     
  • 1
  • 2
  • 3
   col1  col2  col3  col4                   col5
a     1     2     3   cnn        MachineLearning
b     4     5     6   rnn           DeepLearning
c     7     8     9  rcnn  ReinforcementLearning
c     7     8     9  rcnn  ReinforcementLearning

     
     
     
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
print df2.loc['c']
     
     
     
     
  • 1
   col1  col2  col3  col4                   col5
c     7     8     9  rcnn  ReinforcementLearning
c     7     8     9  rcnn  ReinforcementLearning

     
     
     
     
  • 1
  • 2
  • 3
  • 4

DataFrame对象的合并

# DataFrame      
df_a = pd.DataFrame(['wang','jing','hui','is','a','master'],columns=['col6'],index=['a','b','c','d','e','f'])
print df_a
     
     
     
     
  • 1
  • 2
  • 3
     col6
a    wang
b    jing
c     hui
d      is
e       a
f  master

     
     
     
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
#     ,   dfb      
dfb = pd.DataFrame([1,2,4,5,6,7],columns=['col1'],index=['a','b','c','d','f','g'])
print dfb.join(df_a)
     
     
     
     
  • 1
  • 2
  • 3
   col1    col6
a     1    wang
b     2    jing
c     4     hui
d     5      is
f     6  master
g     7     NaN

     
     
     
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
#                
#        how,       
print dfb.join(df_a,how='inner')   #     DataFrame     
     
     
     
     
  • 1
  • 2
  • 3
   col1    col6
a     1    wang
b     2    jing
c     4     hui
d     5      is
f     6  master

     
     
     
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
#     DataFrame     
print dfb.join(df_a,how='outer')
     
     
     
     
  • 1
  • 2
   col1    col6
a   1.0    wang
b   2.0    jing
c   4.0     hui
d   5.0      is
e   NaN       a
f   6.0  master
g   7.0     NaN

     
     
     
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
        
            
감사 하 다.https://blog.csdn.net/u014281392/article/details/75331570

좋은 웹페이지 즐겨찾기