Reputation:
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?
Upvotes: 2
Views: 1307
Reputation: 23099
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
Upvotes: 2
Reputation: 150745
You can do:
# enumerate the NaN values
s = (df.Callsign.isna().cumsum() + 1).astype(int)
df['Callsign'] = df['Callsign'].fillna('Other' + s.astype(str))
Upvotes: 3