Reputation: 1
I need to perform many simple operations on multiple DataFrames. I've tried to use for loop, however code is not working:
for df in (df1, df2):
df=df.replace({':':''}, regex=True)
df["2018"] = pd.to_numeric(df["2018"], downcast="float")
df["2019"] = pd.to_numeric(df["2019"], downcast="float")
I can run it without errors but DataFrames don't change.
Upvotes: 0
Views: 542
Reputation: 346
The reason is df
is a new variable, your change of its content will remain, but the change of itself value will be discard as it will be released.
Also here you cannot find the change of the content after the execution, because after the first line in the loop, df
points to another temporary object, not the original object such as df1
.
Following should work:
for df in (df1, df2):
df.replace({':':''}, regex=True, inplace=True)
df["2018"] = pd.to_numeric(df["2018"], downcast="float")
df["2019"] = pd.to_numeric(df["2019"], downcast="float")
Upvotes: 2
Reputation: 71
from my experience, I would create a simple function that takes dataframe as input and performs operations and returns updated dataframe then loop through a list of dataframes and call the function. something like this.!
def simple_operations(df):
new_df = df.replace({':':''}, regex=True)
new_df["2018"] = pd.to_numeric(df["2018"], downcast="float")
new_df["2019"] = pd.to_numeric(df["2019"], downcast="float")
return new_df
[simple_operations(df) for df in df_list]
Upvotes: 2