0

Got Dataframe df

State   Item        Space   Date
AAA     Grape       0.125   2022-02-11
        Beans       0.0
AAA     Mango       0.25    2022-02-11
AAA     Beetroot    0.375   2022-02-11
        Carrot      0.5

Need to fill empty values in rows with that column's unique value. Tried df['State'].fillna(df['State'].unique(), inplace=True) , didn't worked out.

Expected Output:

State   Item        Space   Date
AAA     Grape       0.125   2022-02-11
AAA     Beans       0.0     2022-02-11
AAA     Mango       0.25    2022-02-11
AAA     Beetroot    0.375   2022-02-11
AAA     Carrot      0.5     2022-02-11
user12345
  • 499
  • 1
  • 5
  • 21

1 Answers1

1

If want replace each column by first non missing values first replace empty strings to missing values and then forward and backfill missing values:

df = df.replace('', np.nan)

df = df.ffill().bfill()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252