How Do I Use A Specific Column's Value In A Pandas Dataframe Where Clause?
I'm trying to select all cells in a pandas DataFrame that meet a certain criteria when a specific column also meets a separate criteria. Given the following DataFrame: A B
Solution 1:
This should work directly, but pandas doesn't have a broadcasting and operator (will happenin 0.14). Here's a workaround.
In[74]: dfOut[74]:
ABCD1/101011/221111/330101/410121/510111/620211/73523
This is a where operation, essentially put np.nan
where the condition is False
In [78]: x = df[df>df.shift(1)]
In [79]: x
Out[79]:
A B C D
1/1NaNNaNNaNNaN1/22NaN1NaN1/33NaNNaNNaN1/4NaNNaNNaN21/5NaNNaNNaNNaN1/62NaN2NaN1/735NaN3
Select by the 2nd condition
In[80]: x[df.D>1]Out[80]:
ABCD1/4NaNNaNNaN21/735NaN3
Solution 2:
I think the problem is actually that the boolean array from the shift operation is one short of the the other conditional. Try adding a false to the first conditional at index zero you should then be able to combine the two conditionals.
I'd the problem really is with the second conditional could you post the result of
DF.dtypes
it looks like it's not int type given the nan array error
Post a Comment for "How Do I Use A Specific Column's Value In A Pandas Dataframe Where Clause?"