Maryo David
Maryo David

Reputation: 589

Deleting values in the column based upon condition in Pandas

I have a dataframe like the following. If the value in the column (HW Category) is "19. Out of Warranty, Expired" then I wanted to empty the value in the column (IO Stat). How can we achieve this result?

Actual Dataframe:

,IO Stat,HW Category
0,Disabled,"19. Out of Warranty, Expired"
1,Disabled,In Use
2,Disabled,In Use
3,Disabled,Onsite
4,Disabled,
5,Disabled,
6,Disabled,
7,Disabled,"19. Out of Warranty, Expired"
8,Disabled,
9,Disabled,
10,Disabled,In Use
11,Disabled,In Use
12,Disabled,Onsite
13,Disabled,
14,Disabled,
15,Disabled,
16,Disabled,"19. Out of Warranty, Expired"
17,Disabled,
18,Disabled,"19. Out of Warranty, Expired"

Expected Result:

,IO Stat,HW Category
0,,"19. Out of Warranty, Expired"
1,Disabled,In Use
2,Disabled,In Use
3,Disabled,Onsite
4,Disabled,
5,Disabled,
6,Disabled,
7,,"19. Out of Warranty, Expired"
8,Disabled,
9,Disabled,
10,Disabled,In Use
11,Disabled,In Use
12,Disabled,Onsite
13,Disabled,
14,Disabled,
15,Disabled,
16,,"19. Out of Warranty, Expired"
17,Disabled,
18,,"19. Out of Warranty, Expired"

Upvotes: 0

Views: 33

Answers (1)

SeaBean
SeaBean

Reputation: 23227

You can use .loc with the first parameter as boolean mask of the filtering condition and the second parameter as the column label of the column to modify:

df.loc[df['HW Category'] == '19. Out of Warranty, Expired', 'IO Stat'] = ''

Result:

print(df)



     IO Stat                   HW Category
0             19. Out of Warranty, Expired
1   Disabled                        In Use
2   Disabled                        In Use
3   Disabled                        Onsite
4   Disabled                           NaN
5   Disabled                           NaN
6   Disabled                           NaN
7             19. Out of Warranty, Expired
8   Disabled                           NaN
9   Disabled                           NaN
10  Disabled                        In Use
11  Disabled                        In Use
12  Disabled                        Onsite
13  Disabled                           NaN
14  Disabled                           NaN
15  Disabled                           NaN
16            19. Out of Warranty, Expired
17  Disabled                           NaN
18            19. Out of Warranty, Expired

Upvotes: 1

Related Questions