zoukankan      html  css  js  c++  java
  • Python 的 pandas 实践

    Python 的 pandas 实践:

      1 # !/usr/bin/env python
      2 # encoding: utf-8
      3 __author__ = 'Administrator'
      4 import pandas as pd
      5 import numpy as np
      6 import matplotlib.pyplot as plt
      7 
      8 
      9 #一、创建对象
     10 #1. 通过传递一个list对象来创建一个Series,pandas会默认创建整型索引:
     11 s=pd.Series([1,3,4,np.nan,6,8])
     12 print(s)
     13 # 0    1.0
     14 # 1    3.0
     15 # 2    4.0
     16 # 3    NaN
     17 # 4    6.0
     18 # 5    8.0
     19 # dtype: float64
     20 
     21 #2.通过传递一个numpy array,时间索引以及列标签来创建一个DataFrame:
     22 dates=pd.date_range('20180301',periods=6)
     23 print(dates)
     24 # DatetimeIndex(['2018-03-01', '2018-03-02', '2018-03-03', '2018-03-04',
     25 #                '2018-03-05', '2018-03-06'],
     26 #               dtype='datetime64[ns]', freq='D')
     27 df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))
     28 # numpy.random.randn(d0, d1, …, dn)是从标准正态分布中返回一个或多个样本值。(可含负数)
     29 # numpy.random.rand(d0, d1, …, dn)的随机样本位于[0, 1)中。
     30 #P=numpy.random.rand(N,K) #随机生成一个 N行 K列的矩阵
     31 print(df)
     32 #                    A         B         C         D
     33 # 2018-03-01 -0.451506 -0.884044 -0.916664 -0.763684
     34 # 2018-03-02 -0.463568  0.340688 -0.077484 -0.237660
     35 # 2018-03-03 -1.533427  0.301283  0.268640 -0.011027
     36 # 2018-03-04  1.036050  0.402203  0.485365  2.086525
     37 # 2018-03-05  0.221578 -0.821756 -0.265241  0.277563
     38 # 2018-03-06  1.774195 -0.288553  1.527936  0.119153
     39 
     40 # '''
     41 
     42 #3.通过传递一个能够被转换成类似序列结构的字典对象来创建一个DataFrame:
     43 df2=pd.DataFrame({
     44     'A':1.,
     45     'B':pd.Timestamp('20180301'),
     46     'C':pd.Series(1,index=list(range(4)),dtype='float32'),
     47     'D':np.array([3]*4,dtype='int32'),
     48     'E':pd.Categorical(["test","train","test","train"]),
     49     'F':'foo'})
     50 print(df2)
     51 #      A          B    C  D      E    F
     52 # 0  1.0 2018-03-01  1.0  3   test  foo
     53 # 1  1.0 2018-03-01  1.0  3  train  foo
     54 # 2  1.0 2018-03-01  1.0  3   test  foo
     55 # 3  1.0 2018-03-01  1.0  3  train  foo
     56 
     57 #4.查看不同列的数据类型:
     58 print(df2.dtypes)
     59 # A           float64
     60 # B    datetime64[ns]
     61 # C           float32
     62 # D             int32
     63 # E          category
     64 # F            object
     65 # dtype: object
     66 
     67 #二、查看数据
     68 #1. 查看dataframe中头部和尾部的行:
     69 print(df.head())
     70 #                    A         B         C         D
     71 # 2018-03-01 -0.250132 -1.403066  1.234990 -3.077763
     72 # 2018-03-02  0.387496 -0.389183  0.186663  1.124608
     73 # 2018-03-03 -0.105463 -0.230739 -0.227575  0.308565
     74 # 2018-03-04 -1.703507  0.194876  1.790366 -0.561566
     75 # 2018-03-05 -0.511609  0.695915  0.398392  0.107062
     76 print(df.tail(3))
     77 #                    A         B         C         D
     78 # 2018-03-04  0.704065  0.492649  0.533961 -1.518723
     79 # 2018-03-05  2.192819 -0.508099 -0.173966 -0.401864
     80 # 2018-03-06 -0.839634 -0.314676 -0.808266 -0.578229
     81 
     82 #2.显示索引、列和底层的numpy数据:
     83 print(df.index)
     84 # DatetimeIndex(['2018-03-01', '2018-03-02', '2018-03-03', '2018-03-04',
     85 #                '2018-03-05', '2018-03-06'],
     86 #               dtype='datetime64[ns]', freq='D')
     87 print(df.columns)
     88 #Index(['A', 'B', 'C', 'D'], dtype='object')
     89 print(df.values)
     90 # [[ 1.65612186 -0.47932887  0.9673593  -0.63872414]
     91 #  [ 0.12229686  0.08831358  1.07344126 -0.12742276]
     92 #  [ 0.54654075  0.77281164 -0.6396787   0.1585142 ]
     93 #  [-0.70695944 -2.12273423 -0.24549759 -0.09530991]
     94 #  [ 2.66920788  0.6520858   1.72857641 -1.34418643]
     95 #  [ 1.87333346 -0.42716996  0.49558928 -1.47606701]]
     96 
     97 #3. describe()函数对于数据的快速统计汇总:
     98 print(df.describe())
     99 #               A         B         C         D
    100 # count  6.000000  6.000000  6.000000  6.000000
    101 # mean   0.399068  0.339270  0.755588 -0.459344
    102 # std    0.890360  1.011113  0.851783  1.759264
    103 # min   -1.002101 -0.806772 -0.333761 -2.411582
    104 # 25%   -0.087757 -0.400563  0.338822 -1.782221
    105 # 50%    0.577418  0.244011  0.502612 -0.622453
    106 # 75%    1.096592  0.941454  1.376095  0.433235
    107 # max    1.281508  1.795854  1.910586  2.284103
    108 
    109 #4. 对数据的转置:
    110 print(df.T)
    111 #    2018-03-01  2018-03-02  2018-03-03  2018-03-04  2018-03-05  2018-03-06
    112 # A    0.843347   -0.906826   -0.528945    1.186650   -1.839152   -0.508169
    113 # B   -0.105481    2.084689   -1.106710    0.521137    0.741946    0.399700
    114 # C   -0.786144    0.269116   -0.180710    3.345385    1.310786   -0.204216
    115 # D    0.453731   -0.243617    0.701440    2.541094    1.337923   -0.673128
    116 
    117 #5. 按轴进行排序
    118 print(df.sort_index(axis=1,ascending=False)) #  axis = 0是按行进行操作, axis=1是按列进行操作;  ascending=False是只递减,否则递增
    119 #                    D         C         B         A
    120 # 2018-03-01  0.389294 -0.227394  0.649234  0.639820
    121 # 2018-03-02  0.680265  0.466626 -1.940228  0.843753
    122 # 2018-03-03  1.520800  0.570192  1.244427 -0.715080
    123 # 2018-03-04  0.309068 -0.224222 -0.226254  1.416381
    124 # 2018-03-05 -1.854131 -0.403245 -0.017054  0.840840
    125 # 2018-03-06 -1.991173  1.275825  0.913996  1.561550
    126 
    127 #6. 按值进行排序
    128 # print(df.sort(column='B')) #?? AttributeError: 'DataFrame' object has no attribute 'sort'
    129 
    130 #三、选择
    131 # 虽然标准的Python/Numpy的选择和设置表达式都能够直接派上用场,
    132 # 但是作为工程使用的代码,我们推荐使用经过优化的pandas数据访问方式: .at, .iat, .loc, .iloc 和 .ix
    133 #(一)获取:
    134 #1. 选择一个单独的列,这将会返回一个Series,等同于 df.A:
    135 print(df['A'])
    136 # 2018-03-01    0.156236
    137 # 2018-03-02   -0.041257
    138 # 2018-03-03   -0.970551
    139 # 2018-03-04   -1.751839
    140 # 2018-03-05    1.521352
    141 # 2018-03-06    0.828690
    142 # Freq: D, Name: A, dtype: float64
    143 
    144 #2. 通过[]进行选择,这将会对行进行切片
    145 print(df[0:3])
    146 #                    A         B         C         D
    147 # 2018-03-01 -0.432011  0.697033 -3.028116 -0.217882
    148 # 2018-03-02 -1.744071  0.647694  1.031179 -1.043985
    149 # 2018-03-03 -0.673125  0.689913  0.648986 -1.471825
    150 print(df['20180302':'20180304'])
    151 #                    A         B         C         D
    152 # 2018-03-02 -0.803947  0.147807 -0.248534  0.496719
    153 # 2018-03-03 -1.518123  0.376390 -0.793349  0.612074
    154 # 2018-03-04  0.146634  0.506102  1.316693 -0.801691
    155 
    156 #(二)通过标签选择:
    157 #1. 使用标签来获取一个交叉的区域:
    158 print(df.loc[dates[0]])
    159 # A   -1.593039
    160 # B    0.400735
    161 # C   -0.870638
    162 # D   -0.551766
    163 # Name: 2018-03-01 00:00:00, dtype: float64
    164 #2. 通过标签来在多个轴上进行选择:
    165 print(df.loc[:,['A','B']])
    166 #                    A         B
    167 # 2018-03-01  0.326446  0.633246
    168 # 2018-03-02  0.169674  0.892832
    169 # 2018-03-03 -0.755691 -2.028912
    170 # 2018-03-04 -1.005360  0.529193
    171 # 2018-03-05 -0.457140  0.842211
    172 # 2018-03-06  0.343157  0.879763
    173 
    174 #3. 标签切片
    175 print(df.loc['20180302':'20180304',['A','B']])
    176 #                    A         B
    177 # 2018-03-02  0.197173  0.040377
    178 # 2018-03-03  2.064367  1.112152
    179 # 2018-03-04  0.888216 -0.591129
    180 
    181 #4. 对于返回的对象进行维度缩减
    182 print(df.loc['20180302',['A','B']])
    183 # A   -0.259955
    184 # B   -0.019266
    185 # Name: 2018-03-02 00:00:00, dtype: float64
    186 
    187 #5. 获取一个标量
    188 print(df.loc[dates[0],'A']) #-0.313259346223
    189 
    190 #6. 快速访问一个标量(与上一个方法等价)
    191 print(df.at[dates[0],'A'])  #-0.313259346223
    192 
    193 #(三)通过位置选择:
    194 #1. 通过传递数值进行位置选择(选择的是行)
    195 print(df.iloc[3])
    196 # A    1.661488
    197 # B   -1.175748
    198 # C    0.642823
    199 # D   -0.491914
    200 # Name: 2018-03-04 00:00:00, dtype: float64
    201 
    202 #2. 通过数值进行切片,与numpy/python 中的情况类似
    203 print(df.iloc[3:5,0:2]) #选择第3、第4行,第1、第2列
    204 #                    A         B
    205 # 2018-03-04  0.492426  0.412712
    206 # 2018-03-05  0.541252 -0.009380
    207 
    208 #3. 通过制定一个位置的列表,与numpy/python中的情况类似
    209 print(df.iloc[[1,2,4],[0,2]])
    210 #                    A         C
    211 # 2018-03-02 -0.638074  1.794516
    212 # 2018-03-03 -0.403471 -0.934373
    213 # 2018-03-05 -1.309320  1.353276
    214 
    215 #4. 对行进行切片
    216 print(df.iloc[1:3,:])
    217 #                    A         B         C         D
    218 # 2018-03-02  1.980513 -0.218688  2.627449  1.314947
    219 # 2018-03-03 -0.532379  1.382092 -1.270961  0.722475
    220 
    221 #5. 对列进行切片
    222 print(df.iloc[:,1:3])
    223 #                    B         C
    224 # 2018-03-01  0.332228 -1.682811
    225 # 2018-03-02 -0.533398 -0.254960
    226 # 2018-03-03 -0.926688  0.890513
    227 # 2018-03-04 -0.448742  0.763850
    228 # 2018-03-05 -0.841622  0.514873
    229 # 2018-03-06 -1.346557  1.516414
    230 
    231 #6. 获取特定的值
    232 print(df.iloc[1,1]) #0.481882236461
    233 print(df.iat[1,1]) #0.481882236461
    234 
    235 
    236 
    237 #(四)布尔索引:
    238 #1. 使用一个单独列的值来选择数据:
    239 print(df[df.A>0])
    240 #                    A         B         C         D
    241 # 2018-03-02  0.566243  1.510954 -0.898180  0.856439
    242 # 2018-03-03  1.008447 -1.597226 -0.665134 -0.287472
    243 # 2018-03-05  0.952498 -0.144979  0.620468 -0.830652
    244 
    245 #2. 使用where操作来选择数据:
    246 print(df[df>0])
    247 #                    A         B         C         D
    248 # 2018-03-01  0.892660       NaN       NaN       NaN
    249 # 2018-03-02  1.512600       NaN       NaN  1.375527
    250 # 2018-03-03  0.970026  1.184603  1.182990       NaN
    251 # 2018-03-04  1.913993       NaN  0.914778  0.137170
    252 # 2018-03-05  0.482589       NaN       NaN  0.668817
    253 # 2018-03-06       NaN  0.539344  0.142892       NaN
    254 
    255 #3. 使用isin()方法来过滤:
    256 df2=df.copy()
    257 df2['E']=['one','one','two','three','four','three']
    258 print(df2)
    259 #                    A         B         C         D      E
    260 # 2018-03-01 -1.138724  0.566583  0.338254  2.072839    one
    261 # 2018-03-02 -0.366949  0.335546  1.653024  1.445071    one
    262 # 2018-03-03  0.724615  1.715933 -0.754757 -1.452252    two
    263 # 2018-03-04 -0.881962 -0.173858 -0.340868 -0.556665  three
    264 # 2018-03-05 -2.126513 -0.113010 -0.796566  0.210673   four
    265 # 2018-03-06  0.716490  0.223395 -1.428238  0.328406  three
    266 print(df2[df2['E'].isin(['two','four'])])
    267 #                    A         B         C         D     E
    268 # 2018-03-03 -0.737833 -1.161520  0.897204 -0.029158   two
    269 # 2018-03-05  1.072054  1.234587  0.935680 -1.284542  four
    270 
    271 
    272 
    273 #(五)设置:
    274 #1. 设置一个新的列:
    275 s1=pd.Series([1,2,3,4,5,6],index=pd.date_range('20180302',periods=6))
    276 print(s1)
    277 # 2018-03-02    1
    278 # 2018-03-03    2
    279 # 2018-03-04    3
    280 # 2018-03-05    4
    281 # 2018-03-06    5
    282 # 2018-03-07    6
    283 # Freq: D, dtype: int64
    284 df['F']=s1
    285 print(df)
    286 #                    A         B         C         D    F
    287 # 2018-03-01  2.413592 -0.336264  0.165597  2.143270  NaN
    288 # 2018-03-02 -1.921596 -2.100707 -0.454461  0.563247  1.0
    289 # 2018-03-03 -0.235034 -0.517009 -2.409731 -0.711854  2.0
    290 # 2018-03-04  0.667604 -0.838737 -0.425916 -0.238519  3.0
    291 # 2018-03-05  1.057415  1.457143  0.440690  0.948613  4.0
    292 # 2018-03-06  0.539187 -0.952633  0.316752  0.422146  5.0
    293 
    294 #2. 通过标签设置新的值:
    295 df.at[dates[0],'A']=0
    296 
    297 #3. 通过位置设置新的值:
    298 df.iat[0,1]=0
    299 
    300 #4. 通过一个numpy数组设置一组新值:
    301 df.loc[:,'D']=np.array([5]*len(df))
    302 print(df)
    303 #                    A         B         C  D    F
    304 # 2018-03-01  0.000000  0.000000  0.164267  5  NaN
    305 # 2018-03-02  0.614534 -0.865975 -0.977389  5  1.0
    306 # 2018-03-03 -0.253095 -1.451951  2.360233  5  2.0
    307 # 2018-03-04  0.143115  0.363544  1.587648  5  3.0
    308 # 2018-03-05  0.010932  0.802590 -1.701589  5  4.0
    309 # 2018-03-06 -0.354579  0.830066  0.404646  5  5.0
    310 
    311 #5. 通过where操作来设置新的值:
    312 df2=df.copy()
    313 df2[df2>0]=-df2
    314 print(df2)
    315 #                    A         B         C  D    F
    316 # 2018-03-01  0.000000  0.000000 -1.385454 -5  NaN
    317 # 2018-03-02 -0.773506 -0.444692 -0.620307 -5 -1.0
    318 # 2018-03-03 -0.506590 -2.445527 -0.664229 -5 -2.0
    319 # 2018-03-04 -0.568711 -0.709224 -2.582502 -5 -3.0
    320 # 2018-03-05 -1.074985 -2.480905 -0.537869 -5 -4.0
    321 # 2018-03-06 -2.659346 -1.055430 -0.379758 -5 -5.0
    322 
    323 
    324 
    325 #四、缺失值处理
    326 # 在pandas中,使用np.nan来代替缺失值,这些值将默认不会包含在计算中,详情请参阅:Missing Data Section。
    327 #1. reindex()方法可以对指定轴上的索引进行改变/增加/删除操作,这将返回原始数据的一个拷贝:
    328 df1=df.reindex(index=dates[0:4],columns=list(df.columns)+['E'])
    329 df1.loc[dates[0]:dates[1],'E']=1
    330 print(df1)
    331 #                    A         B         C         D    E
    332 # 2018-03-01 -0.275255 -0.290044  0.707118  1.094318  1.0
    333 # 2018-03-02 -1.340747  0.633546 -0.911210 -0.275105  1.0
    334 # 2018-03-03 -1.044219  0.659945  1.370910  0.262282  NaN
    335 # 2018-03-04 -0.015582  1.540852 -0.792882 -0.380751  NaN
    336 
    337 #2. 去掉包含缺失值的行:
    338 # df1=df1.dropna(how='any')
    339 # print(df1)
    340 # #                    A         B         C         D    E
    341 # 2018-03-01 -0.914568  0.784980 -1.698139 -0.096874  1.0
    342 # 2018-03-02 -0.410249 -0.494166  0.932946 -0.467547  1.0
    343 
    344 #3. 对缺失值进行填充:
    345 df1=df1.fillna(value=5)
    346 print(df1)
    347 #                    A         B         C         D    E
    348 # 2018-03-01 -1.265605  0.778767 -0.947968 -1.330982  1.0
    349 # 2018-03-02  1.778973 -1.428542  1.257860  0.362724  1.0
    350 # 2018-03-03 -1.589094 -0.517478 -0.164942 -0.507224  5.0
    351 # 2018-03-04  2.363145  2.089114 -0.081683 -0.184851  5.0
    352 
    353 #4.对数据进行布尔填充
    354 df1=pd.isnull(df1)
    355 print(df1)
    356 #                 A      B      C      D      E
    357 # 2018-03-01  False  False  False  False  False
    358 # 2018-03-02  False  False  False  False  False
    359 # 2018-03-03  False  False  False  False  False
    360 # 2018-03-04  False  False  False  False  False
    361 
    362 
    363 
    364 
    365 #五、相关操作
    366 # (一)统计(相关操作通常情况下不包括缺失值)
    367 # #1. 执行描述性统计:
    368 print(df.mean())
    369 # A   -0.066441
    370 # B    0.154609
    371 # C   -0.154372
    372 # D   -0.155221
    373 # dtype: float64
    374 
    375 #2. 在其他轴上进行相同的操作:
    376 print(df.mean(1))
    377 # 2018-03-01   -0.138352
    378 # 2018-03-02   -0.226558
    379 # 2018-03-03    0.121705
    380 # 2018-03-04    0.855662
    381 # 2018-03-05   -0.892621
    382 # 2018-03-06    0.062726
    383 # Freq: D, dtype: float64
    384 
    385 #3.对于拥有不同维度,需要对齐的对象进行操作。Pandas会自动的沿着指定的维度进行广播:
    386 
    387 
    388 # (二)Apply
    389 #1. 对数据应用函数:
    390 print(df)
    391 print(df.apply(np.cumsum))
    392 #                    A         B         C         D
    393 # 2018-03-01 -0.381460 -0.296346  1.229803 -1.300226
    394 # 2018-03-02  0.365891  0.974026  1.570268 -2.572981
    395 # 2018-03-03  0.624070  0.211935  0.635084 -1.110378
    396 # 2018-03-04  2.945062 -0.406832 -0.043918 -0.470773
    397 # 2018-03-05  3.542080  0.092974 -1.585544 -0.658267
    398 # 2018-03-06  3.440084  0.448828 -2.400617 -0.734055
    399 print(df.apply(lambda x:x.max()-x.min()))
    400 # A    2.702452
    401 # B    2.032463
    402 # C    2.771429
    403 # D    2.762828
    404 # dtype: float64
    405 
    406 # (三)直方图
    407 s=pd.Series(np.random.randint(0,7,size=10))
    408 print(s)
    409 # 0    2
    410 # 1    6
    411 # 2    6
    412 # 3    3
    413 # 4    3
    414 # 5    4
    415 # 6    4
    416 # 7    6
    417 # 8    6
    418 # 9    2
    419 # dtype: int32
    420 print(s.value_counts())
    421 # 6    4
    422 # 4    2
    423 # 3    2
    424 # 2    2
    425 # dtype: int64
    426 
    427 
    428 # (四)字符串方法
    429 # Series对象在其str属性中配备了一组字符串处理方法,可以很容易的应用到数组中的每个元素,如下段代码所示。
    430 s=pd.Series(['A','B','C','Aaba','Baca',np.nan,'CABA','dog','cat'])
    431 print(s.str.lower())
    432 # 0       a
    433 # 1       b
    434 # 2       c
    435 # 3    aaba
    436 # 4    baca
    437 # 5     NaN
    438 # 6    caba
    439 # 7     dog
    440 # 8     cat
    441 # dtype: object
    442 
    443 
    444 
    445 #六、合并
    446 #Pandas提供了大量的方法能够轻松的对Series,DataFrame和Panel对象进行各种符合各种逻辑关系的合并操作。
    447 #1、Concat
    448 df=pd.DataFrame(np.random.randn(10,4))
    449 print(df)
    450 #           0         1         2         3
    451 # 0  0.620744 -0.921194  0.130483 -0.305914
    452 # 1  0.311699 -0.085041  0.638297 -0.077868
    453 # 2  0.327473 -0.732598 -0.134463  0.498805
    454 # 3 -0.622715 -0.819375 -0.473504 -0.379117
    455 # 4 -1.309207 -0.794917 -1.284665  0.830677
    456 # 5 -1.170121 -2.063048 -0.836381  0.925829
    457 # 6 -0.766342  0.454018 -0.181846 -1.052607
    458 # 7 -0.996856  0.189226  0.428375 -1.149523
    459 # 8  1.080517  1.884718 -0.065141 -0.781686
    460 # 9  0.087353  0.209678 -1.333989  0.863220
    461 
    462 #break it into pieces
    463 pieces=[df[:3],df[3:7],df[7:]]
    464 print(pieces)
    465 print(pd.concat(pieces))
    466 #           0         1         2         3
    467 # 0  1.187009 -0.493550  0.777065  1.494107
    468 # 1 -0.915190  1.228669  0.216910  1.610432
    469 # 2 -0.647737  1.961472  1.369682 -1.195257
    470 # 3  1.474973  1.968576  1.282678 -1.798167
    471 # 4  1.449858 -1.828631 -0.217424  0.992141
    472 # 5 -1.056223  0.464964  0.135468  0.181781
    473 # 6 -1.677772  1.456419  0.642563 -0.895238
    474 # 7  0.123780  0.030988  1.960217  0.140918
    475 # 8  1.071418  1.737486 -0.170948  0.859271
    476 # 9 -0.056640 -1.439686 -0.358960 -1.765060
    477 
    478 
    479 #2、Join .类似于SQL类型的合并。
    480 left=pd.DataFrame({'key':['foo','foo'],'lval':[1,2]})
    481 print(left)
    482 #    key  lval
    483 # 0  foo     1
    484 # 1  foo     2
    485 right=pd.DataFrame({'key':['foo','foo'],'rval':[4,5]})
    486 print(right)
    487 #    key  rval
    488 # 0  foo     4
    489 # 1  foo     5
    490 pd1=pd.merge(left,right,on='key')
    491 print(pd1)
    492 #    key  lval  rval
    493 # 0  foo     1     4
    494 # 1  foo     1     5
    495 # 2  foo     2     4
    496 # 3  foo     2     5
    497 
    498 #3、Append。将一行连接到一个DataFrame上。
    499 df=pd.DataFrame(np.random.randn(8,4),columns=['A','B','C','D'])
    500 print(df)
    501 #           A         B         C         D
    502 # 0  0.205671 -1.236797 -1.127111  1.422836
    503 # 1  0.646151  0.202197 -0.160218 -0.839145
    504 # 2  1.479783 -0.678455  0.649959 -1.085791
    505 # 3 -0.851987 -0.821248  0.125836  0.819543
    506 # 4 -1.312988 -0.898903 -0.420592  1.672173
    507 # 5  0.240516 -0.711331 -0.717536  0.620066
    508 # 6 -0.442280  0.539277 -1.428910  1.060193
    509 # 7  0.257239 -2.034086  1.121833  1.518571
    510 s=df.iloc[3]
    511 df1=df.append(s,ignore_index=True)
    512 print(df1)
    513 #           A         B         C         D
    514 # 0  0.205671 -1.236797 -1.127111  1.422836
    515 # 1  0.646151  0.202197 -0.160218 -0.839145
    516 # 2  1.479783 -0.678455  0.649959 -1.085791
    517 # 3 -0.851987 -0.821248  0.125836  0.819543
    518 # 4 -1.312988 -0.898903 -0.420592  1.672173
    519 # 5  0.240516 -0.711331 -0.717536  0.620066
    520 # 6 -0.442280  0.539277 -1.428910  1.060193
    521 # 7  0.257239 -2.034086  1.121833  1.518571
    522 # 8 -0.851987 -0.821248  0.125836  0.819543
    523 
    524 
    525 #七、分组
    526 #对于“group by”操作,我们通常是指以下一个或多个操作步骤:
    527 # * (splitting)按照一些规则将数据分为不同的组;
    528 # * (applying)对于每组数据分别执行一个函数;
    529 # * (combining)将结果组合到一个数据结构中;
    530 
    531 df=pd.DataFrame({'A':['foo','bar','foo','bar','foo','bar','foo','foo'],
    532                  'B':['one','one','two','three','two','two','one','three'],
    533                  'C':np.random.randn(8),
    534                  'D':np.random.randn(8) })
    535 print(df)
    536 #      A      B         C         D
    537 # 0  foo    one  0.792610  0.153922
    538 # 1  bar    one  1.497661  0.548711
    539 # 2  foo    two  0.038679  1.100214
    540 # 3  bar  three -1.074874  0.238335
    541 # 4  foo    two  1.176477  1.260415
    542 # 5  bar    two -0.629367 -1.098556
    543 # 6  foo    one  0.015918 -1.646855
    544 # 7  foo  three -0.486434 -0.930165
    545 
    546 #1、分组并对每个分组执行sum函数:
    547 dfg=df.groupby('A').sum()
    548 print(dfg)
    549 #            C         D
    550 # A
    551 # bar -0.20658 -0.311509
    552 # foo  1.53725 -0.062469
    553 #2、通过多个列进行分组形成一个层次索引,然后执行函数:
    554 dfg2=df.groupby(['A','B']).sum()
    555 print(dfg2)
    556 #                   C         D
    557 # A   B
    558 # bar one    1.497661  0.548711
    559 #     three -1.074874  0.238335
    560 #     two   -0.629367 -1.098556
    561 # foo one    0.808528 -1.492933
    562 #     three -0.486434 -0.930165
    563 #     two    1.215156  2.360629
    564 
    565 #八、Reshapeing
    566 #1、Stack
    567 tuples=list(zip(*[['bar','bar','baz','baz','foo','foo','quz','quz'],
    568                   ['one','two','one','two','one','two','one','two']]))
    569 index=pd.MultiIndex.from_tuples(tuples,names=['first','second'])
    570 df=pd.DataFrame(np.random.randn(8,2),index=index,columns=['A','B'])
    571 df2=df[:4]
    572 print(df2)
    573 #                      A         B
    574 # first second
    575 # bar   one     1.146806  0.413660
    576 #       two    -0.241280 -0.756498
    577 # baz   one    -0.429149 -1.598932
    578 #       two     0.103805 -2.092773
    579 
    580 stacked=df2.stack()
    581 print(stacked)
    582 # first  second
    583 # bar    one     A   -0.671894
    584 #                B    0.488440
    585 #        two     A   -0.085894
    586 #                B   -0.888060
    587 # baz    one     A   -0.647487
    588 #                B   -1.573074
    589 #        two     A    0.084324
    590 #                B   -0.216785
    591 # dtype: float64
    592 
    593 stacked0=stacked.unstack()
    594 print(stacked0)
    595 #                      A         B
    596 # first second
    597 # bar   one    -2.281352  0.683124
    598 #       two    -2.555841  0.020481
    599 # baz   one     1.007699 -0.605463
    600 #       two     1.177308  0.833826
    601 stacked1=stacked.unstack(1)
    602 print(stacked1)
    603 # second        one       two
    604 # first
    605 # bar   A -2.281352 -2.555841
    606 #       B  0.683124  0.020481
    607 # baz   A  1.007699  1.177308
    608 #       B -0.605463  0.833826
    609 stacked2=stacked.unstack(0)
    610 print(stacked2)
    611 # first          bar       baz
    612 # second
    613 # one    A -0.279379  0.011654
    614 #        B  0.713347  0.482510
    615 # two    A -0.980093  0.536366
    616 #        B -0.378279 -1.023949
    617 
    618 #2、数据透视表
    619 df=pd.DataFrame({'A':['one','one','two','three']*3,
    620                  'B':['A','B','C']*4,
    621                  'C':['foo','foo','foo','bar','bar','bar']*2,
    622                  'D':np.random.randn(12),
    623                  'E':np.random.randn(12) })
    624 print(df)
    625 #         A  B    C         D         E
    626 # 0     one  A  foo -1.037929 -0.967839
    627 # 1     one  B  foo  0.143201  1.936801
    628 # 2     two  C  foo -1.108452  1.350176
    629 # 3   three  A  bar  0.696497  0.578974
    630 # 4     one  B  bar -1.206393  1.218049
    631 # 5     one  C  bar -0.814728  0.440277
    632 # 6     two  A  foo -2.039865 -1.298114
    633 # 7   three  B  foo -0.155810 -0.249138
    634 # 8     one  C  foo -0.436593  0.548266
    635 # 9     one  A  bar -2.236853 -1.218478
    636 # 10    two  B  bar -0.542738 -1.018322
    637 # 11  three  C  bar -0.657995 -0.772053
    638 #可以从这个数据中轻松的生成数据透视表:
    639 pdtable=pd.pivot_table(df,values='D',index=['A','B'],columns=['C'])
    640 print(pdtable)
    641 # C             bar       foo
    642 # A     B
    643 # one   A  0.878124  0.739554
    644 #       B  1.508778 -0.261956
    645 #       C  0.452780  0.850025
    646 # three A -0.616593       NaN
    647 #       B       NaN -0.924248
    648 #       C -0.778909       NaN
    649 # two   A       NaN -0.249317
    650 #       B  0.341066       NaN
    651 #       C       NaN  0.706030
    652 # '''
    653 #九、时间序列
    654 #Pandas在对频率转换进行重新采样时拥有简单、强大且高效的功能(如将按秒采样的数据转换为按5分钟为单位进行采样的数据)。这种操作在金融领域非常常见。
    655 # rng=pd.date_range('1/1/2018',periods=100,freq='S')
    656 # ts=pd.Series(np.random.randint(0,500,len(rng)),index=rng)
    657 # ts0=ts.resample('5Min',how='sum')
    658 # ........
    659 # ........
    660 
    661 #十、Categorical
    662 #从0.15版本开始,pandas可以在DataFrame中支持Categorical类型的数据
    663 
    664 #1、将原始的grade转换为Categorical数据类型:
    665 # ........
    666 # ........
    667 
    668 #十一、画图
    669 ts=pd.Series(np.random.randn(1000),index=pd.date_range('1/1/2018',periods=1000))
    670 ts=ts.cumsum()
    671 ts.plot()
    672 # ........
    673 # ........
    674 
    675 #十二、导入和保存数据
    676 #(一)CSV
    677 #1、写入 csv文件
    678 df.to_csv('foo.csv')
    679 #2、从CSV文件中读取:
    680 pd.read_csv('foo.csv')
    681 
    682 #(二)HDF5
    683 #1、
    684 # ........
    685 # ........
    686 
    687 #(三)Excel
    688 #1、写入excel文件:
    689 df.to_excel('foo.xlsx',sheet_name='Sheet1')
    690 #2、从excel文件中读取:
    691 pd.read_excel('foo.xlsx','Sheet1',index_col=None,na_values=['NA'])
    【Reference】
    1、十分钟搞定pandas
    2、
    10 Minutes to pandas


  • 相关阅读:
    vue 点击出现下拉菜单,点击下拉菜单以外都要关闭菜单
    关于SET ANSI_PADDING的用法
    VUE中的/deep/用法
    VUE中的/deep/用法
    数字与字符串系列教材 (十)- 自己开发一个Java StringBuffer
    数字与字符串系列教材 (九)- Java StringBuffer常见方法
    数字与字符串系列教材 (八)- Java 比较字符串详解
    数字与字符串系列教材 (七)- Java常见字符串方法
    数字与字符串系列教材 (六)- Java中的字符串String详解
    数字与字符串系列教材 (六)- Java中的字符串String详解
  • 原文地址:https://www.cnblogs.com/shenxiaolin/p/8672866.html
Copyright © 2011-2022 走看看