Pandas Equivalent of R's which()

user2643394

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 I'd like to set.

In R, what I'm trying to do is:

df[which(df[,colnumber] > somenumberIchoose),]

The which() function finds indices of row entries in a column in the dataframe which are greater than somenumberIchoose, and returns this as a vector. Then, I slice the dataframe by using these row indices to indicate which rows of the dataframe I would like to look at in the new form.

Is there an equivalent way to do this in python? I've seen references to enumerate, which I don't fully understand after reading the documentation. My sample in order to get the row indices right now looks like this:

indexfuture = [ x.index(), x in enumerate(df['colname']) if x > yesterday]  

However, I keep on getting an invalid syntax error. I can hack a workaround by for looping through the values, and manually doing the search myself, but that seems extremely non-pythonic and inefficient.

What exactly does enumerate() do? What is the pythonic way of finding indices of values in a vector that fulfill desired parameters?

Note: I'm using Pandas for the dataframes

fdeheeger

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Equivalent of R's factor function in Pandas

From Dev

Equivalent of transform in R/ddply in Python/pandas?

From Dev

pandas equivalent of Stata's encode

From Dev

equivalent of R's View for Python's pandas

From Dev

Python equivalent of R's head and tail function

From Dev

What is python's equivalent of R's NA?

From Dev

What's the R equivalent of progn in lisp?

From Dev

R's read.table equivalent in Python

From Dev

Is there an equivalent to R's negative indexing in Matlab?

From Dev

Python's equivalent for R's dput() function

From Dev

Python equivalent for R's 'zoo' package

From Dev

Python numpy or pandas equivalent of the R function sweep()

From Dev

What is the equivalent of SQL's IN keyword in R?

From Dev

R dcast equivalent in python pandas

From Dev

Equivalent of R's createDataPartition in Python

From Dev

What's the equivalent of `cons` in R?

From Dev

Equivalent of R function 'ave' in Python Pandas

From Dev

pandas equivalent of R's cbind (concatenate/stack vectors vertically)

From Dev

Equivalent of R's removeSparseTerms in Python

From Dev

What is the R equivalent of pandas .resample() method?

From Dev

Equivalent of R rbind.fill in Python Pandas

From Dev

Is there a Python equivalent to R's sample() function?

From Dev

R equivalent to MATLAB's 'tokens' option in regexp

From Dev

Pandas equivalent of Python's readlines function

From Dev

pandas equivalent for R dcast

From Dev

pandas: function equivalent to SQL's datediff()?

From Dev

Python equivalent of R's rnbinom parametrized with mu

From Dev

R's which() and which.min() Equivalent in Python

From Dev

while(*s++=*t++) is equivalent to which expression?

Related Related

HotTag

Archive