1

Edited version of original post

I have a dataframe that's entirely populated with NaN. Specifically:

print(histPrice['Allocation %'])

[Output]: 
 Symbols    SPY  GLD  TLT   MA   QQQ      TIP
Date                                         
2019-11-01  NaN  NaN  NaN   NaN  NaN      NaN
2019-10-31  NaN  NaN  NaN   NaN  NaN      NaN
2019-10-30  NaN  NaN  NaN   NaN  NaN      NaN
2019-10-29  NaN  NaN  NaN   NaN  NaN      NaN
2019-10-28  NaN  NaN  NaN   NaN  NaN      NaN
2019-10-25  NaN  NaN  NaN   NaN  NaN      NaN
2019-10-24  NaN  NaN  NaN   NaN  NaN      NaN
2019-10-23  NaN  NaN  NaN   NaN  NaN      NaN
2019-10-22  NaN  NaN  NaN   NaN  NaN      NaN
2019-10-21  NaN  NaN  NaN   NaN  NaN      NaN
2019-10-18  NaN  NaN  NaN   NaN  NaN      NaN

I have the following numpy array:

x = np.array([0.1,0.3,0.2,0.1,0.1,0.2])

I tried assigning the array to 2019-10-30 row by:

histPrice['Allocation %'].iloc[2] = x 

as well as

histPrice['Allocation %'].iloc[2,:] = x

Yet, both result in:

print(histPrice['Allocation %'].iloc[2])

[Output]: 
 Symbols
SPY       NaN
GLD       NaN
TLT       NaN
MA        NaN
QQQ       NaN
TIP       NaN
Name: 2019-10-30 00:00:00, dtype: float64 

I'm baffled as to why it's still outputting NaN. Any help would be greatly appreciated.

7
  • are u trying to replace every third row of table with np array? Commented Nov 4, 2019 at 3:44
  • @Tserenjamts i'm just trying to replace a specific row with an array Commented Nov 4, 2019 at 13:37
  • histPrice is of what type? Commented Nov 4, 2019 at 15:11
  • @aprilangel histPrice is 'pandas.core.frame.DataFrame' Commented Nov 5, 2019 at 2:19
  • 1
    @Drive2blue but it looks like histPrice['Allocation %'] is a dataframe as well Commented Nov 5, 2019 at 3:39

4 Answers 4

2

You need loc with indexing of df.index:

histPrice.loc[df.index[2], 'Allocation %'] = x 
Sign up to request clarification or add additional context in comments.

Comments

0

You should do

df.iloc[2,:]=a[0]
df
Out[142]: 
        Stock: Bond: Gold:
2/01/19    NaN   NaN   NaN
1/31/19    NaN   NaN   NaN
1/30/19    33%   33%   33%
1/29/19    NaN   NaN   NaN

1 Comment

This didn't work. See section "Additional Details" in my updated post
0

If you want to update the whole row with your numpy array, you should be able to use:

x = np.array([2,3,1])
df.iloc[2]= x

        Stock Bond Gold
2/01/19   NaN  NaN  NaN
1/31/19   NaN  NaN  NaN
1/30/19     2    3    1
1/29/19   NaN  NaN  NaN

This will update the row with each value of the array, and not just the first value.

I am curious if this will work for you?

import pandas as pd
import numpy as np
df = pd.DataFrame(columns=['Stock','Bond','Gold'], index=['2/01/19','1/31/19','1/30/19','1/29/19'])
histPrice = {}
histPrice['Allocation %'] = df
x = np.array([2,3,1])
histPrice['Allocation %'].iloc[2] = x 
print(histPrice['Allocation %'].iloc[2])

11 Comments

This didn't work. See section "Additional Details" in my updated post
@drive2blue It looks like you are trying to assign the array to one column histPrice['Allocation %'], and not a row.
@drive2blue do you have a dataframe saved in a dictionary?
@Drive2blue Based on your updated question, I am wondering if you have one or more dataframes stored in a dictionary. Is there a chance you are updating the wrong dataframe (I only ask because you reference two different dataframes in your updated question: histPrice['Allocation %'], histPrice['RP Allocation %'])?
@Drive2blue I was wondering if there was a nested dataframe, and I was even looking into storing dataframes within dataframes as that is a new concept for me. I'm glad I was still able to help though.
|
0

From the description, you are storing a dataframe in another single row dataframe.

And when doing below, You are setting an value in the copy of your dataframe.

histPrice['Allocation %'].iloc[2] = x 

Instead, try: (you can replace the 0 with your index)

histPrice.loc[0, 'Allocation %'].iloc[2] = x

2 Comments

hmmm, I tried your suggestion but got KeyError: 'Allocation %' Instead, I tried histPrice.loc[ : , 'Allocation %'].iloc[2] = x but that resulted in ValueError: setting an array element with a sequence :(
@Drive2blue don't use array, set x as a list. should be fine =)

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.