I am trying to transform elements in various data frames (standardize numerical values to be between 0 and 1, one-hot encode categorical variables) but when I try to overwrite the dataframe in a loop it doesn't modify the existing dataframe, only the loop variable. Here is a dummy example:
t = pd.DataFrame(np.arange(1, 16).reshape(5, 3))
b = pd.DataFrame(np.arange(1, 16).reshape(5, 3))
for hi in [t, b]:
hi = pd.DataFrame(np.arange(30, 45).reshape(5, 3))
But when I run this code both t and b have their original values. How can I overwrite the original dataframe (t or b) while in a loop?
The specific problem I'm running into is when trying to use get_dummies function in the loop:
hi = pd.get_dummies(hi, columns=['column1'])
tandbanywhere?map()orapply()to update Seriestorbinside the loop either, so you shouldn't expect them to change.hi). You can find a SO answer that deals with this here