Issue
I have the following DataFrame:
Periodicity | Answer |
---|---|
M | Yes |
M | YeS |
Y | yeS |
M | No |
Y | NO |
M | nO |
I need to filter the DataFrame to get the rows that have a Monthly (M) periodicity and have a positive (YES, YEs, Yes, YeS, and so on) answer.
I have tried filter it with the following code:
import pandas as pd
import re
data = {'Periodicity': ['M', 'Y', 'M', 'M', 'M', 'Y', 'M', 'M'],
'Answer': ['YES', 'Yes', 'YEs', 'NO', 'no', 'No', 'yeS', 'yeS']}
df = pd.DataFrame(data)
pat=r'^[Yy].*'
df_filter=df[df.Answer.str.contains(pat)]
But I dont know how to add another condition to filter the DataFrame to match the desired Periodicity. Everytime I add another filter condition, I get the following error message:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all
().
Solution
Try this:
df_filter=df[df.Answer.str.contains(pat) & df.Periodicity.str.contains('M')]
Answered By - vitali_droujko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.