Home > Enterprise >  My Existing Dataframe has NaN or blank values in column 'Code', I want to replace the NaN
My Existing Dataframe has NaN or blank values in column 'Code', I want to replace the NaN

Time:07-08

[![enter image description here][1]][1]

[![enter image description here][2]][2]

This is my current Dataframe: [1]: https://i.stack.imgur.com/xn6N1.png

This is my expected Output: [2]: https://i.stack.imgur.com/1tMX5.png

CodePudding user response:

import pandas as pd
import numpy as np

new_data = {
  "Name": [],
  "Code": []
}
for index, row in df.iterrows():
    name = row["Name"]
    code = row["Code"]
    new_data["Name"].append(name)
    if(row["Code"] is np.nan):
        new_data["Code"].append(f"{name}_Deault")
    else: 
        new_data["Code"].append(code)

new_df = pd.DataFrame(new_data)
    

That may work for your case.

  • Related