Python dataframe replace last n rows with a list of n elements

Question:

I have a data frame of 2500 rows. I am trying to replace last n rows a dataframe with a list of n elements. I am giving an example of my problem and what I wanted

df = 
      A
0    10.5
1    10.5
2    10.5
3    10.5
4    10.5
5    10.5
6    10.5

My new list has two elements to be placed at the bottom two rows.

op_res = [20.5, 30.5]

My code and present output:

df.loc[-2:,'A'] = pd.Series(op_res)
df = 
      A
0    10.5
1    10.5
2    10.5
3    10.5
4    10.5
5    nan
6    nan

What could be wrong in my code?
My code and present output:

df = 
      A
0    10.5
1    10.5
2    10.5
3    10.5
4    10.5
5    20.5
6    30.5
Asked By: Mainland

||

Answers:

We can use DataFrame.iloc and broadcast the list to an array with numpy.array:

df.iloc[-len(op_res):] = np.array(op_res)[:, None]


      A
0  10.5
1  10.5
2  10.5
3  10.5
4  10.5
5  20.5
6  30.5

Or with DataFrame.append:

df.iloc[:-len(op_res)].append(pd.DataFrame({'A': op_res}), ignore_index=True)

      A
0  10.5
1  10.5
2  10.5
3  10.5
4  10.5
5  20.5
6  30.5
Answered By: Erfan

Try this:

df.A.to_numpy()[-2:]=op_res
df
      A
0  10.5
1  10.5
2  10.5
3  10.5
4  10.5
5  20.5
6  30.5

The problem in your code: index not match, so when assign it back, it will return nan, since pandas assign will match index as always

pd.Series(op_res)
0    20.5
1    30.5
dtype: float64

Index from original df

df.iloc[-2:,0] 
5    10.5
6    10.5
Name: A, dtype: float64

From above, we know the index [0,1] can not match with index [5,6] so all assign value will return nan.

Answered By: BENY
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.