Select Pandas rows based on list index

前端 未结 5 625
滥情空心
滥情空心 2020-11-28 03:37

I have a dataframe df :

   20060930  10.103       NaN     10.103   7.981
   20061231  15.915       NaN     15.915  12.686
   20070331   3.196       NaN               


        
相关标签:
5条回答
  • 2020-11-28 03:55

    Another way (although it is a longer code) but it is faster than the above codes. Check it using %timeit function:

    df[df.index.isin([1,3])]
    

    PS: You figure out the reason

    0 讨论(0)
  • 2020-11-28 03:57

    There are many ways of solving this problem, and the ones listed above are the most commonly used ways of achieving the solution. I want to add two more ways, just in case someone is looking for an alternative.

    index_list = [1,3]
    
    df.take(pos)
    
    #or
    
    df.query('index in @index_list')
    
    0 讨论(0)
  • 2020-11-28 04:00

    For large datasets, it is memory efficient to read only selected rows via the skiprows parameter.

    Example

    pred = lambda x: x not in [1, 3]
    pd.read_csv("data.csv", skiprows=pred, index_col=0, names=...)
    

    This will now return a DataFrame from a file that skips all rows except 1 and 3.


    Details

    From the docs:

    skiprows : list-like or integer or callable, default None

    ...

    If callable, the callable function will be evaluated against the row indices, returning True if the row should be skipped and False otherwise. An example of a valid callable argument would be lambda x: x in [0, 2]

    This feature works in version pandas 0.20.0+. See also the corresponding issue and a related post.

    0 讨论(0)
  • 2020-11-28 04:10
    ind_list = [1, 3]
    df.ix[ind_list]
    

    should do the trick! When I index with data frames I always use the .ix() method. Its so much easier and more flexible...

    UPDATE This is no longer the accepted method for indexing. The ix method is deprecated. Use .iloc for integer based indexing and .loc for label based indexing.

    0 讨论(0)
  • 2020-11-28 04:20

    you can also use iloc:

    df.iloc[[1,3],:]
    

    This will not work if the indexes in your dataframe do not correspond to the order of the rows due to prior computations. In that case use:

    df.index.isin([1,3])
    

    ... as suggested in other responses.

    0 讨论(0)
提交回复
热议问题