1

I have a dataframe df with 200 rows, and numpy array my_array with 15 values.

my_array = [41892.79355875, 40239.97933262, 39466.32169404, 38416.39545664,
            40012.3803004, 41135.45946026, 43084.18917943, 44825.08405799,
            44066.70603561, 46636.34415037, 45855.25783352, 45863.87118957,
            44697.45547342, 48065.5708295, 47931.83508874]

When I add the values of my_array into df under new column column_2, the 15 values get added into the first 15 rows of df.

df['column_2'] = pd.DataFrame(my_array, columns=['column_2'])

How do I make the code add the values of my_array into the last 15 rows of df?

1 Answer 1

1

For now another problem is that you also erase all others values of the column, you may not set a DataFrame but just the array as the new value.

To set values in a column, at specific index, use df.loc[df.index[#], 'NAME']

import numpy as np
import pandas as pd

df = pd.DataFrame([[1, 2] for _ in range(100)], columns=['column_1', 'column_2'])
my_array = np.array([41892.79355875, 40239.97933262, 39466.32169404, 38416.39545664,
                     40012.3803004, 41135.45946026, 43084.18917943, 44825.08405799,
                     44066.70603561, 46636.34415037, 45855.25783352, 45863.87118957,
                     44697.45547342, 48065.5708295, 47931.83508874])

df.loc[df.index[-15:], 'column_2'] = my_array

print(df)
    column_1      column_2
0          1      2.000000
1          1      2.000000
2          1      2.000000
3          1      2.000000
4          1      2.000000
..       ...           ...
95         1  45855.257834
96         1  45863.871190
97         1  44697.455473
98         1  48065.570830
99         1  47931.835089
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you, could you look at the edited post to see how I can place the values in an already existing column?
please help with this
@LisaS I don't really get exactly what you ask
I want the values being displayed in Predicted to be displayed in Prediction which has Nan values right now
stackoverflow.com/questions/50066608/… like this but I don't know how to do mine
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.