2

enter image description here

Hi, I have a data frame given in the picture above. There are NAN values in the 'Callsign' column and I want to replace the NAN values with "Other" but it must be in incremental format. for eg:

   Callsign
1  Other1
4  Other2

and so on. I am not able to formulate the python code for the specific output. Can anyone help me?

2 Answers 2

3

You can do:

# enumerate the NaN values
s = (df.Callsign.isna().cumsum() + 1).astype(int)

df['Callsign'] = df['Callsign'].fillna('Other' + s.astype(str))
Sign up to request clarification or add additional context in comments.

Comments

2

using .loc and cumsum

df = pd.DataFrame({'Callsign' : [np.nan, 'GENERAL', 'NEXTTIME',np.nan,np.nan]})

   Callsign
0       NaN
1   GENERAL
2  NEXTTIME
3       NaN
4       NaN

df.loc[df["Callsign"].isnull(), "Callsign"] = "Other" + (
    df["Callsign"].isnull().cumsum()
).astype(str)

print(df)

   Callsign
0    Other1
1   GENERAL
2  NEXTTIME
3    Other2
4    Other3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.