I'm working with pandas dataframe and I have a variable that has the values of one of the columns. When I change the values directly in the dataframe, the values stored in the variable are replaced too, is this a bug or is there any logic behind?
The idea is to change the df['b]
's value and keep the values
intact, for other usages.
Here's a sample code:
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.random(size=(100, 2)), columns=['a', 'b'])
values = df['b'].values
peaks = [0, 5, 10, 15, 20, 25]
print(values[peaks])
df['b'][peaks] = np.nan
print(values[peaks])
and the output:
>>> [0.69820632 0.8375975 0.84961463 0.97845189 0.82764414 0.93884249]
>>> [nan nan nan nan nan nan]