简体   繁体   English

在 python dataframe 中用增量值填充 NAN

[英]Fill NAN with incremental values in python dataframe

在此处输入图像描述

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. “Callsign”列中有 NAN 值,我想用“Other”替换 NAN 值,但它必须是增量格式。 for eg:例如:

   Callsign
1  Other1
4  Other2

and so on.等等。 I am not able to formulate the python code for the specific output.我无法为特定的 output 制定 python 代码。 Can anyone help me?谁能帮我?

You can do:你可以做:

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

df['Callsign'] = df['Callsign'].fillna('Other' + s.astype(str))

using .loc and cumsum使用.loccumsum

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM