Search code examples
pythonpandasdataframedata-analysisexploratory-data-analysis

I want to apply multiple filters and change a column value accordingly in pandas [Working now]


Suppose I have a dataframe like so:

Fil1 Fil2                    A    B   C   D 
a    crossdev radio com      Act  1   23  324
b    crossdev webapp radio   Act  4   45  343
a    Streaming webapp radio  Act  3   23  566
a    crossdev com            Act  1   12  746

The Fil1 column in the actual file is really long name that I'm filtering, but here I'm referencing it as just 'a'.

The code I'm using is --

df.loc[(df['Fil1'] == 'a') & (df['Fil2'].str.contains('com')) , 'C'] = 0
df.loc[(df['Fil1'] == 'a') & (df['Fil2'].str.contains('com')) , 'D'] = 0
df.loc[(df['Fil1'] == 'a') & (df['Fil2'].str.contains('com')) , 'A'] = 'Fail'

Outputting this df to excel.

Desired Excel Output:

Fil1 Fil2                    A     B   C   D 
a    crossdev radio com      Fail  1   0   0
b    crossdev webapp radio   Act   4   45  343
a    Streaming webapp radio  Act   3   23  566
a    crossdev com            Fail  1   0   0

My code is not giving me any error but it is not even giving me desired result. Is there any other workaround?


Solution

  • The code is working! It had no error.
    The value that I referred to here as 'a' was a mess in my real dataset that caused the problem.