Home > Software design >  Replace column index with ListComprehension
Replace column index with ListComprehension

Time:12-19

I have a Pandas Dataframe like this

enter image description here

which is related to the patients and their notes about their problem. The column "Problem" contains null values and I want to fill this column values according to the patient's notes.

How can I do this?

I was thinking to do it in this way that if in df["PATİENT NOTE"] we observe "KNEE", then we can replace None with the word KNEE like this:

`df["PROBLEM"].replace("col,"KNEE")` 

or if in df["PATİENT NOTE"] we get the word "ARM", we can use this command to replace the word:

`df["PROBLEM"].replace(col,"ARM")` 

But I don't know how to do this. So, I was wondering if anybody can help me?

I would very much appreciate your help.

CodePudding user response:

Using where():

df["PROBLEM"] = df["PROBLEM"].where(df["PROBLEM"].eq(None), df["PATIENT NOTE"])

CodePudding user response:

You can split PATIENT NOTE column. Then get first string after ON MY:

df.loc[df['PROBLEM'].isnull(),"PROBLEM"] = df['PATIENT NOTE'].str.split('ON MY ').str.get(0) # This only applies to None/nan rows.
  • Related