asymon
asymon

Reputation: 187

Empty columns when inserted into into df from another df

I tried to add from df5 columns to df_prog. But for some reason they remain empty. I do not understand what I'm doing wrong. Code:

df5['Kol1_1Y']

223520    14.0
223521    65.0
223522    13.0
223523    39.0
223524    13.0
223525     3.0
223526    10.0
223527    19.0
223528    16.0
223529    29.0
Name: Kol1_1Y, dtype: float64

df_prog['Kol1_1Y'] = df5['Kol1_1Y']
df_prog['Kol2_1Y'] = df5['Kol2_1Y']
df_prog['Kol1_3M'] = df5['Kol1_3M']
df_prog['Kol2_3M'] = df5['Kol2_3M']
df_prog.to_excel("C:\python\progGB.xlsx")

df_prog

0           RESPR   PREVPR  Kol1_1Y Kol2_1Y Kol1_3M Kol2_3M
0   0.4944  0.4944  1.4894  NaN NaN NaN NaN
1   0.7073  0.7073  3.2020  NaN NaN NaN NaN
2   0.3965  0.3965  -0.3989 NaN NaN NaN NaN
3   0.4501  0.4501  -0.1826 NaN NaN NaN NaN
4   0.0271  0.0271  -6.1202 NaN NaN NaN NaN
5   0.2488  0.2488  -2.8447 NaN NaN NaN NaN
6   0.5190  0.5190  0.0176  NaN NaN NaN NaN
7   0.6667  0.6667  2.2334  NaN NaN NaN NaN
8   0.7708  0.7708  4.5216  NaN NaN NaN NaN
9   0.7074  0.7074  2.9906  NaN NaN NaN NaN

Upvotes: 0

Views: 153

Answers (1)

Andy L.
Andy L.

Reputation: 25259

Pandas = assignment checking both index and columns. In your case, columns is matched, but index is different. Therefore, it assigns all NaN. To ignore index and columns, you need assigning from numpy ndarray such as:

df_prog['Kol1_1Y'] = df5['Kol1_1Y'].values
df_prog['Kol2_1Y'] = df5['Kol2_1Y'].values
df_prog['Kol1_3M'] = df5['Kol1_3M'].values
df_prog['Kol2_3M'] = df5['Kol2_3M'].values

Upvotes: 2

Related Questions