Reputation: 949
I've got a dataframe in which I am trying to look through the column "#Inc" to see if there is the number 4. If there is, I want to print the whole row. How do I do this?
I tried this and failed (Error was --> only list like object are allowed to be passed to isin(), you passed an int):
if df.loc[df['#Inc'].isin(4)] is true:
print 'WIN'
else:
print 'FAILURE!'
Here is the output:
OUTPUT:
Month Avg Inc. #Inc Avg.Dec #Dec
0 JAN 0.091454 1 0.000000 0
1 FEB 0.008307 1 -0.030809 1
2 MAR 0.000000 0 -0.027193 2
3 APR 0.008845 1 -0.035857 1
4 MAY 0.000000 0 -0.076321 2
5 JUN 0.033715 1 -0.025242 1
6 JUL 0.016775 1 -0.028849 1
7 AUG 0.079845 1 -0.033116 1
8 SEP 0.000000 0 -0.042201 2
9 OCT 0.044914 1 -0.049798 1
10 NOV 0.000000 0 -0.148163 2
11 DEC 0.039241 1 -0.024030 1
Upvotes: 0
Views: 679
Reputation: 25
The error is because the method isin() does not accept a single string, int, float value as input parameter.
0.18.x, 0.19.x version of Pandas documentation mentions input parameter of type iterable, pandas dataframe, series or dictionary as the input parameter for method isin(). http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.isin.html
0.17.x version of pandas The documentation for method isin() says that it takes input value as the list, even if you have single string then add that string to a list and then invoke isin() method. http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.Series.isin.html.
I hope this helps. Cheers!!
Upvotes: 0
Reputation: 28858
You can simply apply the filter you want and not assign the result if you work in a shell (either IPython or Normal Python shell):
In [27]: df[df[' #Inc'] == 4]
Out[27]:
Month Avg Inc. #Inc Avg.Dec #Dec
1 FEB 0.008307 4 -0.030809 1
3 APR 0.008845 4 -0.035857 1
You can assign the result of the filter for a later use:
In [35]: interesting_results = df[df[' #Inc'] == 4]
Upvotes: 2
Reputation: 1438
Think you want something simple like the following:
df = pd.DataFrame({'#Inc': [1,2,3,4,5], 'another_col': ['x','y','z','a','b']})
for _, row in df.iterrows():
if row['#Inc'] == 4:
print(row)
What's happening in the above is we're using the generator created by iterrows
to loop through all of the rows in the data frame and then print when a particular key in the row equals the value you're looking for.
Upvotes: 0