美文网首页pandas我爱编程
21天pandas入门(3) - cookbook 1

21天pandas入门(3) - cookbook 1

作者: default | 来源:发表于2016-03-16 19:14 被阅读1065次

    10分钟入门已经写完了,那么基本的东西大概都了解。
    入门以后我们的目标变成了要玩的6,666666666666666666666666
    所以

    cookbook (short and sweet example)


    官网上的描述是short and sweet example,是用python3.4的,其他版本的python可能会需要一点小修改。
    idioms(怎么翻译)

    if-then/if-then-else on one column, and assignment to another one or more columns:
        In [1]: df = pd.DataFrame(
       ...:      {'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]}); df
       ...: 
        Out[1]: 
       AAA  BBB  CCC
        0    4   10  100
        1    5   20   50
        2    6   30  -30
        3    7   40  -50
    

    if-then

        In [2]: df.ix[df.AAA >= 5,'BBB'] = -1; df
        Out[2]: 
           AAA  BBB  CCC
        0    4   10  100
        1    5   -1   50
        2    6   -1  -30
        3    7   -1  -50
        
        In [3]: df.ix[df.AAA >= 5,['BBB','CCC']] = 555; df
        Out[3]: 
           AAA  BBB  CCC
        0    4   10  100
        1    5  555  555
        2    6  555  555
        3    7  555  555
    
        In [4]: df.ix[df.AAA < 5,['BBB','CCC']] = 2000; df
        Out[4]: 
           AAA   BBB   CCC
        0    4  2000  2000
        1    5   555   555
        2    6   555   555
        3    7   555   555
    

    或者可以使用 where

        In [5]: df_mask = pd.DataFrame({'AAA' : [True] * 4, 'BBB' : [False] * 4,'CCC' : [True,False] * 2})
        
        In [6]: df.where(df_mask,-1000)
        Out[6]: 
           AAA   BBB   CCC
        0    4 -1000  2000
        1    5 -1000 -1000
        2    6 -1000   555
        3    7 -1000 -1000
    

    或者使用np的where可以if-then-else

        In [7]: df = pd.DataFrame(
           ...:      {'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]}); df
           ...: 
        Out[7]: 
           AAA  BBB  CCC
        0    4   10  100
        1    5   20   50
        2    6   30  -30
        3    7   40  -50
        
        In [8]: df['logic'] = np.where(df['AAA'] > 5,'high','low'); df
        Out[8]: 
           AAA  BBB  CCC logic
        0    4   10  100   low
        1    5   20   50   low
        2    6   30  -30  high
        3    7   40  -50  high
    
    split
    In [9]: df = pd.DataFrame(
       ...:      {'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]}); df
       ...: 
    Out[9]: 
       AAA  BBB  CCC
    0    4   10  100
    1    5   20   50
    2    6   30  -30
    3    7   40  -50
    
    In [10]: dflow = df[df.AAA <= 5]
    
    In [11]: dfhigh = df[df.AAA > 5]
    
    In [12]: dflow; dfhigh
    Out[12]: 
       AAA  BBB  CCC
    2    6   30  -30
    3    7   40  -50
    
    Building Criteria (构造规范/这翻译水平真是够了)

    Select with multi-column criteria

    In [13]: df = pd.DataFrame(
       ....:      {'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]}); df
       ....: 
    Out[13]: 
       AAA  BBB  CCC
    0    4   10  100
    1    5   20   50
    2    6   30  -30
    3    7   40  -50
    

    ...and (without assignment returns a Series) 不赋值返回的就是一个Series

    In [14]: newseries = df.loc[(df['BBB'] < 25) & (df['CCC'] >= -40), 'AAA']; newseries
    Out[14]: 
    0    4
    1    5
    Name: AAA, dtype: int64
    

    ...or (without assignment returns a Series)

    In [15]: newseries = df.loc[(df['BBB'] > 25) | (df['CCC'] >= -40), 'AAA']; newseries;
    

    ...or (with assignment modifies the DataFrame.)

    In [16]: df.loc[(df['BBB'] > 25) | (df['CCC'] >= 75), 'AAA'] = 0.1; df
    Out[16]: 
       AAA  BBB  CCC
    0  0.1   10  100
    1  5.0   20   50
    2  0.1   30  -30
    3  0.1   40  -50
    

    Select rows with data closest to certain value using argsort

    In [17]: df = pd.DataFrame(
       ....:      {'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]}); df
       ....: 
    Out[17]: 
       AAA  BBB  CCC
    0    4   10  100
    1    5   20   50
    2    6   30  -30
    3    7   40  -50
    
    In [18]: aValue = 43.0
    
    In [19]: df.ix[(df.CCC-aValue).abs().argsort()]
    Out[19]: 
       AAA  BBB  CCC
    1    5   20   50
    0    4   10  100
    2    6   30  -30
    3    7   40  -50
    

    Dynamically reduce a list of criteria using a binary operators

    In [20]: df = pd.DataFrame(
       ....:      {'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]}); df
       ....: 
    Out[20]: 
       AAA  BBB  CCC
    0    4   10  100
    1    5   20   50
    2    6   30  -30
    3    7   40  -50
    
    In [21]: Crit1 = df.AAA <= 5.5
    
    In [22]: Crit2 = df.BBB == 10.0
    
    In [23]: Crit3 = df.CCC > -40.0
    
    # One could hard code:
    
    In [24]: AllCrit = Crit1 & Crit2 & Crit3
    # ...Or it can be done with a list of dynamically built criteria
    
    In [25]: CritList = [Crit1,Crit2,Crit3]
    
    In [26]: AllCrit = functools.reduce(lambda x,y: x & y, CritList)
    
    In [27]: df[AllCrit]
    Out[27]: 
       AAA  BBB  CCC
    0    4   10  100
    

    selection

    The indexing docs.
    Using both row labels and value conditionals

    In [28]: df = pd.DataFrame(
       ....:      {'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]}); df
       ....: 
    Out[28]: 
       AAA  BBB  CCC
    0    4   10  100
    1    5   20   50
    2    6   30  -30
    3    7   40  -50
    
    In [29]: df[(df.AAA <= 6) & (df.index.isin([0,2,4]))]
    Out[29]: 
       AAA  BBB  CCC
    0    4   10  100
    2    6   30  -30
    

    Use loc for label-oriented slicing and iloc positional slicing

    In [30]: data = {'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]}
    
    In [31]: df = pd.DataFrame(data=data,index=['foo','bar','boo','kar']); df
    Out[31]: 
         AAA  BBB  CCC
    foo    4   10  100
    bar    5   20   50
    boo    6   30  -30
    kar    7   40  -50
    
    There are 2 explicit slicing methods, with a third general case
    • Positional-oriented (Python slicing style : exclusive of end)
    • Label-oriented (Non-Python slicing style : inclusive of end)
    • General (Either slicing style : depends on if the slice contains labels or positions)
    In [32]: df.loc['bar':'kar'] #Label
    Out[32]: 
         AAA  BBB  CCC
    bar    5   20   50
    boo    6   30  -30
    kar    7   40  -50
    
    # Generic
    In [33]: df.ix[0:3] #Same as .iloc[0:3]
    Out[33]: 
         AAA  BBB  CCC
    foo    4   10  100
    bar    5   20   50
    boo    6   30  -30
    
    In [34]: df.ix['bar':'kar'] #Same as .loc['bar':'kar']
    Out[34]: 
         AAA  BBB  CCC
    bar    5   20   50
    boo    6   30  -30
    kar    7   40  -50
    

    Ambiguity arises when an index consists of integers with a non-zero start or non-unit increment.(所以还是尽量不要这么用,徒增麻烦)

    In [35]: df2 = pd.DataFrame(data=data,index=[1,2,3,4]); #Note index starts at 1.
    
    In [36]: df2.iloc[1:3] #Position-oriented
    Out[36]: 
       AAA  BBB  CCC
    2    5   20   50
    3    6   30  -30
    
    In [37]: df2.loc[1:3] #Label-oriented
    Out[37]: 
       AAA  BBB  CCC
    1    4   10  100
    2    5   20   50
    3    6   30  -30
    
    In [38]: df2.ix[1:3] #General, will mimic loc (label-oriented)
    Out[38]: 
       AAA  BBB  CCC
    1    4   10  100
    2    5   20   50
    3    6   30  -30
    
    In [39]: df2.ix[0:3] #General, will mimic iloc (position-oriented), as loc[0:3] would raise a KeyError
    Out[39]: 
       AAA  BBB  CCC
    1    4   10  100
    2    5   20   50
    3    6   30  -30
    

    Using inverse operator (~) to take the complement of a mask

    In [40]: df = pd.DataFrame(
       ....:      {'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40], 'CCC' : [100,50,-30,-50]}); df
       ....: 
    Out[40]: 
       AAA  BBB  CCC
    0    4   10  100
    1    5   20   50
    2    6   30  -30
    3    7   40  -50
    
    In [41]: df[~((df.AAA <= 6) & (df.index.isin([0,2,4])))]
    Out[41]: 
       AAA  BBB  CCC
    1    5   20   50
    3    7   40  -50
    

    下面是panel,就不学了,跳过去,下一篇学习下面的东西。

    相关文章

      网友评论

        本文标题:21天pandas入门(3) - cookbook 1

        本文链接:https://www.haomeiwen.com/subject/hgnqlttx.html