I have this Pandas dataframe:
BTC_USD_rates = {
"Open": {"01/01/2022": 46217.5, "02/01/2022": 47738.7, "03/01/2022": 47293.9, "04/01/2022": 46435.7, "05/01/2022": 45833.1, "06/01/2022": 43431.6, "07/01/2022": 43097.9, "08/01/2022": 41551.3},
"Low": {"01/01/2022": 46217.5, "02/01/2022": 46718.2, "03/01/2022": 45704.0, "04/01/2022": 45602.1, "05/01/2022": 42535.1, "06/01/2022": 42481.1, "07/01/2022": 40810.0, "08/01/2022": 40574.3},
"High": {"01/01/2022": 47917.6, "02/01/2022": 47944.9, "03/01/2022": 47556.0, "04/01/2022": 47505.4, "05/01/2022": 47019.4, "06/01/2022": 43772.3, "07/01/2022": 43127.7, "08/01/2022": 42304.4},
"Close": {"01/01/2022": 47738.0, "02/01/2022": 47311.8, "03/01/2022": 46430.2, "04/01/2022": 45837.3, "05/01/2022": 43425.9, "06/01/2022": 43097.5, "07/01/2022": 41546.7, "08/01/2022": 41672.0},
"Volume": {"01/01/2022": 31239, "02/01/2022": 27020, "03/01/2022": 41062, "04/01/2022": 55589, "05/01/2022": 83744, "06/01/2022": 63076, "07/01/2022": 88358, "08/01/2022": 52544},
}
df1 = pd.DataFrame.from_dict(BTC_USD_rates)
df1
Open Low High Close Volume
01/01/2022 46217.5 46217.5 47917.6 47738.0 31239
02/01/2022 47738.7 46718.2 47944.9 47311.8 27020
03/01/2022 47293.9 45704.0 47556.0 46430.2 41062
04/01/2022 46435.7 45602.1 47505.4 45837.3 55589
05/01/2022 45833.1 42535.1 47019.4 43425.9 83744
06/01/2022 43431.6 42481.1 43772.3 43097.5 63076
07/01/2022 43097.9 40810.0 43127.7 41546.7 88358
08/01/2022 41551.3 40574.3 42304.4 41672.0 52544
I need to multiply it by the Close
column of this other dataframe df2
as follows:
new_rates
Open Low High Close
01/01/2022 1.4732 1.4732 1.4732 1.4732
02/01/2022 1.4732 1.4732 1.4732 1.4732
03/01/2022 1.4650 1.4583 1.4763 1.4732
04/01/2022 1.4719 1.4651 1.4784 1.4669
05/01/2022 1.4669 1.4669 1.4669 1.4669
06/01/2022 1.4717 1.4708 1.4854 1.4817
07/01/2022 1.4819 1.4733 1.4849 1.4741
08/01/2022 1.4741 1.4741 1.4741 1.4741
df2 = new_rates['Close']
df2
01/01/2022 1.4732
02/01/2022 1.4732
03/01/2022 1.4732
04/01/2022 1.4669
05/01/2022 1.4669
06/01/2022 1.4817
07/01/2022 1.4741
08/01/2022 1.4741
Name: Close, dtype: float64
But when I run this line of code I always end up having df1
with the same result as if I was multiplying all of its elements by 1
:
df1.mul(df2, axis = 0)
df1
Open Low High Close Volume
01/01/2022 46217.5 46217.5 47917.6 47738.0 31239
02/01/2022 47738.7 46718.2 47944.9 47311.8 27020
03/01/2022 47293.9 45704.0 47556.0 46430.2 41062
04/01/2022 46435.7 45602.1 47505.4 45837.3 55589
05/01/2022 45833.1 42535.1 47019.4 43425.9 83744
06/01/2022 43431.6 42481.1 43772.3 43097.5 63076
07/01/2022 43097.9 40810.0 43127.7 41546.7 88358
08/01/2022 41551.3 40574.3 42304.4 41672.0 52544
What am I missing here?
CodePudding user response:
Your df1 variable isn't being altered in memory by the function attribute .mul() Instead it returns the result of the multiplication.
What you need to do is reassign df1 like so,
df1 = df1.mul(df2, axis = 0)
and your df1 will be altered