Home > Net >  update the dataframe value based on if condition
update the dataframe value based on if condition

Time:01-10

i've this snippet (help got my stackoverflow) .

First this is looping into file and splitting based on new line and appending to a list and converting the list into dataFrame and dataFrame to excel.

But my problem is in the third Output column. i wanted to check for a string and if that string contains [Hi sai] it should be same if this string is not present it should update that column as no string found.

Snippet

import pandas as pd
import re

with open(r'report.txt', 'r') as f_in:
    text = f_in.read()

pat = re.compile(r"(?<=\*\*\*$).*?(?=^\*\*\*)", flags=re.S | re.M)

out = []
for group in map(str.strip, pat.findall(text)):
    out.append(group.split("\n", maxsplit=2))
 
df = pd.DataFrame(out, columns=["ID", "Name header", "output"])
    
df.to_excel(r'/final_report.xlsx', index=False)

The above only parses the file and appends list but it not checking for the condition. i tried with this but not sure where to put this. Any help?

if df[~df["output"].str.contains("Hi Sai")]:
    df['output']="No value found"
else:
    pass
``

CodePudding user response:

You should be able to do:

df.loc[~df["output"].str.contains("Hi Sai"),"output"] = "No value found"

CodePudding user response:

You can use "map":

def set_output(o):
    if(o=='Hi Sai'):
         return "No value found"
    return o

df['Output']=df['Output'].map(set_output)
  • Related