python-3.xpandasdataframenanfillna

TypeError: "value" parameter must be a scalar, dict or Series, but you passed a "Index"


df = df['Consequence Number'].fillna("CLS" + df.index.astype(str))

I did try the above solution along with iterating rows

 for index in priority_cls.iterrows():
     if priority_cls['Consequence Number'].isna() == True:
        priority_cls['Consequence Number'] = 'CLS00'+ str(priority_cls['ConsequenceNumber'].isnull().sum())

The error is as follows:

TypeError: "value" parameter must be a scalar, dict or Series, but you passed a "Index"


Solution

  • Convert your Index to_series:

    df['Consequence Number'].fillna("CLS" + df.index.to_series().astype(str))
    

    Example:

    df = pd.DataFrame({'Consequence Number': ['A', 'B', pd.NA, pd.NA]})
    
    df['out'] = (df['Consequence Number']
                 .fillna("CLS" + df.index.to_series().astype(str))
                )
    

    Output:

      Consequence Number   out
    0                  A     A
    1                  B     B
    2               <NA>  CLS2
    3               <NA>  CLS3