Python Forum
How to replace matching rows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to replace matching rows
#1
Hi,
I have below DataFrame, I want to replace if row value contains 'pass' with '0'.

import pandas as pd

df=pd.DataFrame({'Name':['NHAY', 'HAY','UTY','UIK', 'JUI','YTH'],'Status':['Pass','Running','pass(warning 0)','pass','fail','passed']})
df.loc[df['Status'].str.contains('pass', case=False), 'Status'] = 0
but if in case I want to column number (like here in this case 1), how to do it.
Reply
#2
You can do this e.g., as follows:

df.iloc[:, 1][df.iloc[:, 1].str.contains('pass', case=False)] = 0
Reply
#3
I want to add a column and apply the above condition, I use below code but all rows fill with zeros.

df['Add']=df.loc[df['Status'].str.contains('pass', case=False), 'Status'] = 0
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,599 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Merging spreadsheets with the same columns and extracting rows with matching entries johnbernard 3 8,357 Aug-19-2021, 03:08 PM
Last Post: johnbernard
  Partial Matching Rows In Pandas DataFrame Query eddywinch82 1 2,338 Jul-08-2021, 06:32 PM
Last Post: eddywinch82
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,080 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 6,957 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  How to ger matching rows data based on index columns SriRajesh 1 2,171 Mar-08-2019, 11:05 AM
Last Post: scidam
  Search & Replace - Newlines Added After Replace dj99 3 3,355 Jul-22-2018, 01:42 PM
Last Post: buran
  Matching whole words in find/replace script greektranslator 4 13,263 Jul-11-2017, 12:37 PM
Last Post: greektranslator

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020