I need to filter rows on certain conditions on some columns. Those columns are present in a list. Condition will be same for all columns or can be different. For my work, condition is same.
Not working
labels = ['one', 'two', 'three']
df = df [df [x] == 1 for x in labels]
Below code works:
df_list = []
for x in labels:
df_list.append(df[(df [x] == 1)])
df5 = pd.concat(df_list).drop_duplicates()
Creating different dataframes and concating them by avoiding duplicates works.
Expected: It should filter out those rows where value of those column is 1.
Actual: ValueError: too many values to unpack (expected 1)
I understand the reason for the error. Is there any way I can construct the condition by modifying the not working code ?