python - pandas set one column equal to 1 but both df changes -
i'm trying df b column d 1, however, when run code, changes df a column d 1 also... why that, why variables linked? , how change df b only?
import pandas pd, os, numpy np df = pd.dataframe(np.random.randint(0,100,size=(100, 4)), columns=list('abcd')) a=df b=df b['d']=1 output:
>>> b c d 0 98 84 3 1 1 13 35 76 1 2 17 84 28 1 3 22 9 41 1 4 54 3 20 1 >>> b b c d 0 98 84 3 1 1 13 35 76 1 2 17 84 28 1 3 22 9 41 1 4 54 3 20 1 >>>
a, b , df references same object. when change b['d'], changing column of actual object. instead, looks want copy dataframe:
import pandas pd, os, numpy np df = pd.dataframe(np.random.randint(0,100,size=(100, 4)), columns=list('abcd')) a=df.copy() b=df.copy() b['d']=1 which yields
b.head() out: b c d 0 63 52 92 1 1 98 35 43 1 2 24 87 70 1 3 38 4 7 1 4 71 30 25 1 a.head() out: b c d 0 63 52 92 80 1 98 35 43 78 2 24 87 70 26 3 38 4 7 48 4 71 30 25 61 there detailed responses here.
Comments
Post a Comment