Pandas Equivalent of R's which()

前端 未结 6 1606
广开言路
广开言路 2020-12-31 04:12

Variations of this question have been asked before, I\'m still having trouble understanding how to actually slice a python series/pandas dataframe based on conditions that

6条回答
  •  萌比男神i
    2020-12-31 04:38

    I may not understand clearly the question, but it looks like the response is easier than what you think:

    using pandas DataFrame:

    df['colname'] > somenumberIchoose
    

    returns a pandas series with True / False values and the original index of the DataFrame.

    Then you can use that boolean series on the original DataFrame and get the subset you are looking for:

    df[df['colname'] > somenumberIchoose]
    

    should be enough.

    See http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing

提交回复
热议问题