tarkan
tarkan

Reputation: 69

Updating dataframe location with iloc

I am trying to update a data frame row by summing it with another dataframe and storing in that dataframe. For example, if df_1 = x0, x1, x2 and df_2 = y0, y1, y2. I want to add df_1 + df_2 and store this in df_1. My code is listed below and I get an error 'Incompatible indexer with DataFrame'. I noticed I can print(df_weights.iloc[i,:] + new_df) but I can't set that dataframe row equal to that summation...

print('weights:\n',df_weights)
test = df_weights.sum(axis=0)
test = (1 - test)/8
new_df = pd.DataFrame(test)
new_df = new_df.transpose()
print('new df:\n',new_df)
for i in range(0,df_rows):
    df_weights.iloc[i,:] = df_weights.iloc[i,:] + new_df

output

weights:
               0          1          2          3
m00    0.215545 -0.0734553  -0.260289 -0.0204819
mu02  -0.176883  -0.197607   0.194261  -0.024145
mu11   0.295823  -0.239761  -0.177467  -0.216388
mu20  -0.175377   0.166371   0.020717   0.229208
mu03    0.13808  -0.339882 -0.0601851   0.273308
mu12   0.331139  -0.253927  -0.268115    0.30846
mu21  0.0945824   0.126653  -0.176058  0.0993816
mu30  0.0940057  0.0738258  -0.078831  -0.049587
new df:
           0         1         2        3
0  0.022886  0.217223  0.225746  0.05003

Upvotes: 0

Views: 40

Answers (1)

BENY
BENY

Reputation: 323376

Add values at the end to remove the impact of index match

for i in range(0,df_rows):
    df_weights.iloc[i,:] = df_weights.iloc[i,:].values + new_df.values

Upvotes: 1

Related Questions