0

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.

2 Answers 2

2

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]
    
Sign up to request clarification or add additional context in comments.

Comments

2

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")

1 Comment

This is a good answer. If I may add on, we should never reassign the variable (in this case df) that we are iterating over, which would result in another temporary object. And to check whether the object df is the same as the original df1 being iterated over, you can simply do print(df is df1) which would give you False after the original code of df = df.replace({':':''}, regex=True), indicating that an additional temporary object has been created, and the remaining 2 pandas steps would be applied on this temporary object instead of df1 or df2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.