Slice Pandas DataFrame by Row

后端 未结 2 1696
萌比男神i
萌比男神i 2020-12-23 20:31

I am working with survey data loaded from an h5-file as hdf = pandas.HDFStore(\'Survey.h5\') through the pandas package. Within this DataFrame, all

相关标签:
2条回答
  • 2020-12-23 21:10

    If you already know the index you can use .loc:

    In [12]: df = pd.DataFrame({"a": [1,2,3,4,5], "b": [4,5,6,7,8]})
    
    In [13]: df
    Out[13]:
       a  b
    0  1  4
    1  2  5
    2  3  6
    3  4  7
    4  5  8
    
    In [14]: df.loc[[0,2,4]]
    Out[14]:
       a  b
    0  1  4
    2  3  6
    4  5  8
    
    In [15]: df.loc[1:3]
    Out[15]:
       a  b
    1  2  5
    2  3  6
    3  4  7
    
    0 讨论(0)
  • 2020-12-23 21:13
    In [36]: df
    Out[36]:
       A  B  C  D
    a  0  2  6  0
    b  6  1  5  2
    c  0  2  6  0
    d  9  3  2  2
    
    In [37]: rows
    Out[37]: ['a', 'c']
    
    In [38]: df.drop(rows)
    Out[38]:
       A  B  C  D
    b  6  1  5  2
    d  9  3  2  2
    
    In [39]: df[~((df.A == 0) & (df.B == 2) & (df.C == 6) & (df.D == 0))]
    Out[39]:
       A  B  C  D
    b  6  1  5  2
    d  9  3  2  2
    
    In [40]: df.ix[rows]
    Out[40]:
       A  B  C  D
    a  0  2  6  0
    c  0  2  6  0
    
    In [41]: df[((df.A == 0) & (df.B == 2) & (df.C == 6) & (df.D == 0))]
    Out[41]:
       A  B  C  D
    a  0  2  6  0
    c  0  2  6  0
    
    0 讨论(0)
提交回复
热议问题