I have two dataframes and I wish to update column in one based on the another. The problem is that when I update the column, the old dataframe gets rewritten as well.
(One dataframe contains correlation between column and target variable, the other is supposed to show the ranking)
import numpy as np
import pandas as pd
from scipy.stats import pearsonr
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data[:100]
y = iris.target[:100]
clmns = iris.feature_names
out = pd.DataFrame(index=np.arange(0,len(clmns)), columns=['coef'])
feat_coef = pd.DataFrame(columns=['Feature_name','pearson_koef_FM'])
feat_coef['Feature_name'] = clmns
feat_rank = feat_coef
X_np = np.array(X)
y_np = np.array(y)
for idx,name in enumerate(clmns):
out['coef'].loc[idx] = pearsonr(X_np[:,idx], y_np)[0]
feat_coef['pearson_koef_FM'] = np.absolute(out['coef'])
print '----BEFORE----'
print feat_coef
feat_rank['pearson_koef_FM'] = feat_coef['pearson_koef_FM'].rank(ascending=False)
print '----AFTER----'
print feat_coef
Which returns this:
----BEFORE----
Feature_name pearson_koef_FM
0 sepal length (cm) 0.72829
1 sepal width (cm) 0.684019
2 petal length (cm) 0.969955
3 petal width (cm) 0.960158
----AFTER----
Feature_name pearson_koef_FM
0 sepal length (cm) 3.0
1 sepal width (cm) 4.0
2 petal length (cm) 1.0
3 petal width (cm) 2.0
Obviously, I expect the feat_coef remain unchanged. If I print feat_rank, I get correct output. I feel like it has something to do with setting a copy vs view when copying dataframes.
feat_rankis a reference, so replacefeat_rank = feat_coefwithfeat_rank = feat_coef.copy()