given two DataFrames:
#df1
value Symbol Date Type Value
1109914 AUD 2016-01-29 value 64587.9121
1109949 CAD 2016-01-29 value 65357.1429
1109970 CHF 2016-01-29 value 111881.8681
1110012 DX 2016-01-29 value 91256.4103
#df2
volas Symbol Date Type Value
1109915 AUD 2016-01-29 vola 518.5091
1109950 CAD 2016-01-29 vola 360.9474
1109971 CHF 2016-01-29 vola 767.1880
1110013 DX 2016-01-29 vola 495.8913
1110041 EUR 2016-01-29 vola 876.6861
1110062 GBP 2016-01-29 vola 462.6425
If I want to add a columnd to df1 from df2, naturally, I'd try this:
df1["volas"] = df2["Value"]
However, this returns a Series with nan values (and hence fills the new column with such):
1109921 NaN
1110089 NaN
1110138 NaN
1110159 NaN
1110257 NaN
1110450 NaN
1110618 NaN
1110667 NaN
1110688 NaN
However, if I do this:
market_values['Volas'] = [i for i in market_volas.Value]
The values are assigned as expected.
Why? I simply cannot see where the problem is, especially because my initial attempt is suggested here.
There is problem that index in df1 is different as index in df2.
If length od indexes is equal, you can use values:
df1["volas"] = df2["Value"].values
print (df1)
value Symbol Date Type Value volas
0 1109914 AUD 2016-01-29 value 64587.9121 518.5091
1 1109949 CAD 2016-01-29 value 65357.1429 360.9474
2 1109970 CHF 2016-01-29 value 111881.8681 767.1880
3 1110012 DX 2016-01-29 value 91256.4103 495.8913
Think of
df1["volas"] = df2["Value"]
as doing a merge on the index of the two dataframes. What you're probably after is an actual merge based on some matching columns, e.g.
df1.merge(df2, on=["Symbol","Date"])
which will add the columns from df2 based on matches with the other columns, or, if they are of the same length
df1["volas"] = df2.Value.values
which is basically your loop solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With