Skip to content
Advertisement

Create new column in Pandas and fill down with a specific value

I have a dataset, df, where I would like to create a new column to my dataset and fill down this column with a specific value

Data

id       start  stat
d_in     din    8
d_in     din    8
hello       
hi       din    8
        

Desired

plan    id      start   stat
21      d_in    din     8
21      d_in    din     8
21      hello       
21      hi      din     8

Doing

df['plan'] = df['plan'].fillna('21')

However, this is not actually creating the new column. Any suggestion is appreciated.

Advertisement

Answer

Simply try: df['plan'] = '21'

Or df['plan'] = 21 if an integer type is wanted.

Pandas will automatically broadcast scalars to all rows.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement