I have a pandas dataframe df:
A B C
1 2 NaN
2 3 NaN
4 5 NaN
How can I add a numpy array with the shape of 1 element less then the length of the dataframe, in this case, the numpy array:
[10 20]
And I want to start at the second row and fill the column C:
A B C
1 2 NaN
2 3 10
4 5 20
>Solution :
You can use:
l = [10, 20]
df.loc[df.index[1:1+len(l)], 'C'] = l
Or with iloc:
df.iloc[1:1+len(l), df.columns.get_loc('C')] = l
Output:
A B C
0 1 2 NaN
1 2 3 10.0
2 4 5 20.0