Reputation: 33
I am trying to make a simple function in python that checks a column for a specific value (using a boolean index).
I only want to keep the "True" values.
I was thinking something like the below code, but i am having a hard time with my logic, being a noob and all.
def checkVal(df, col, val):
res = df[col] == val
if val == True:
'''take only the true'''
else:
'''drop'''
The expected output should be a dataframe only holding the true values.
Upvotes: 1
Views: 1255
Reputation: 364
If df[col]
contains Boolean values, you can simply do:
df = df[df['col']]
Upvotes: 2
Reputation: 8033
Why don't you just do the following? This is much simpler & preferred than dropping, especially if you want a new dataframe
df1 = df.loc[df['col'] == val]
Upvotes: 1