Select certain rows (condition met), but only some columns in Python/Numpy

前端 未结 5 2088
一整个雨季
一整个雨季 2020-12-15 05:29

I have an numpy array with 4 columns and want to select columns 1, 3 and 4, where the value of the second column meets a certain condition (i.e. a fixed value). I tried to f

5条回答
  •  醉酒成梦
    2020-12-15 05:57

    I am hoping this answers your question but a piece of script I have implemented using pandas is:

    df_targetrows = df.loc[df[col2filter]*somecondition*, [col1,col2,...,coln]]
    

    For example,

    targets = stockdf.loc[stockdf['rtns'] > .04, ['symbol','date','rtns']]
    

    this will return a dataframe with only columns ['symbol','date','rtns'] from stockdf where the row value of rtns satisfies, stockdf['rtns'] > .04

    hope this helps

提交回复
热议问题