Categories
Mastering Development

Is there a way for me to write my own functions and perform them inplace like most methods in Pandas

I would like my functions to perform certain operations to my pandas DataFrame in place. Most methods in Pandas have the inplace methods, right now, I would like to use to use the idea of the inplace functionality to my function to make this particular function perform in place in order to avoid me reassigning it or thinking I have re-assigned it.

Here is my code in action:

def populate_df(df_month, working_df, Win_ID ="WIN_ID"):
    details = ["Type", "protype", "direction", "per_typ", "produkt_name"]

    for detail in details:
        working_df[detail] = [dict(zip(df_month[Win_ID], df_month[detail]))[id] for id in working_df[Win_ID]]

    return working_df

Here I am trying to apply the function

df1 = travel_details(df_month = Dec2019, working_df=df1)

I would like to do this and it updates my dataframe instead of the assignment

travel_details(df_month = Dec2019, working_df=df1)

or create a boolean like update_inplace in the function that updates when true like:

travel_details(df_month = Dec2019, working_df=df1, update_inplace=True)

Leave a Reply

Your email address will not be published. Required fields are marked *