Pandas how to add value to an existing data-frame by index

327 Views Asked by At

I have an example data frame let's call it df. I want to add more numbers to df but i don't want to start adding after NaN's which will be the index 7 i want to start adding from index 3.

    year        number      letter
0   1945        10          a
1   1950        15          b
2   1955        20          c
3   1960        NaN         NaN
4   1965        NaN         Nan
5   1970        NaN         Nan
6   1975        NaN         Nan

Let's say we have a column like this:

    number2
0   25
1   30
2   35
3   40

my target is to get a df like this

 year        number      letter
 0  1945        10          a
 1  1950        15          b
 2  1955        20          c
 3  1960        25         NaN
 4  1965        30         Nan
 5  1970        35         Nan
 6  1975        40         Nan

I hope I explained it well enough. Thank you for your support !

1

There are 1 best solutions below

1
On BEST ANSWER
number2 = [25,30,35,40]
df.loc[df.number.isna(), 'number'] = number2

Result df:

enter image description here