0

I'm trying to cast a series of 20 values at the end of a dataframe with more than 20 rows. The original values are coming from a numpy array 'Y_pred':

[[3495.47227957]
 [3493.27865109]
 [3491.08502262]
 [3488.89139414]
 [3486.69776567]
 [3484.50413719]
 [3482.31050871]
 [3480.11688024]
 [3477.92325176]
 [3475.72962329]
 [3473.53599481]
 [3471.34236633]
 [3469.14873786]
 [3466.95510938]
 [3464.7614809 ]
 [3462.56785243]
 [3460.37422395]
 [3458.18059548]
 [3455.986967  ]
 [3453.79333852]]

creating column Y_pred and trying to cast the converted series:

df['Y_pred'] = np.nan
df.Y_pred.iloc[-len(Y_pred):].append(pd.Series({'Y_pred': Y_pred}), ignore_index=True)

result is that all rows are NaN

I tried as well this:

series = pd.Series(Y_pred[:, 0])
df.Y_pred.iloc[-20:].append(series, ignore_index=True)

and

df['Y_pred'].append(Y_pred)

nothing works. How to do it properly?

stanvooz
  • 522
  • 3
  • 19
  • `append` is not an in place operation. It _always_ returns a new copy of the DataFrame or series. – Henry Ecker Sep 18 '21 at 20:29
  • @HenryEcker I get a `ValueError: Invalid value NaN (not a number)` – stanvooz Sep 18 '21 at 20:31
  • sorry it was some other problem, but I still have NaN. nothing has been cast – stanvooz Sep 18 '21 at 20:35
  • What do you mean "cast" you don't appear to be converting Data types here? You're trying to overwrite the last `n` rows from your DataFrame from Y_pred? Like in [Python dataframe replace last n rows with a list of n elements](https://stackoverflow.com/q/61297944/15497888) -> `df.iloc[-20:, df.columns.get_loc('Y_pred')] = Y_pred`? – Henry Ecker Sep 18 '21 at 20:36
  • 1
    `df.iloc[-20:, df.columns.get_loc('Y_pred')] = Y_pred`works!!! Thank you! – stanvooz Sep 18 '21 at 20:43

0 Answers0