How to apply a custom function to the same dataframe multiple times in python?

user14504471

I am trying to explode columns of a pandas dataframe to make new columns.

def explode(child_df, column_value):
    child_df = child_df.dropna(subset=[column_value])

    if isinstance(child_df[column_value].iloc[0], str):
        print('tried')
        child_df[column_value] = child_df[column_value].apply(ast.literal_eval)

    expanded_child_df = (pd.concat({i: json_normalize(x) for i, x in child_df.pop(column_value).items()}).reset_index(level=1, drop=True).join(child_df,how='right',lsuffix='_left',rsuffix='_right').reset_index(drop=True))
    expanded_child_df.columns = map(str.lower, expanded_child_df.columns)

    return expanded_child_df 

Is there a way to apply the explode function to a dataframe multiple times,

this is where i'm tryin to apply explode function to the dataframe consolidated_df:

def clean():
    column_value = ['tracking_results','trackable_items','events']
    consolidated_df_cleaner = explode(consolidated_df,column_value.value)
    # Need to iterate over column_value and pass the value as the second argument into `explode` function on the same dataframe
    consolidated_df_cleaner.to_csv('/home/response4.csv',index=False)

tried this but wont work :

pd_list = []
    for param in column_value:
        pd_list.append(apply(explode(consolidated_df),param))

this is what i'm doing right now and i need to avoid this :

consolidated_df_cleaner=explode(consolidated_df,'tracking_results')
consolidated_df_cleaner2=explode(consolidated_df_cleaner,'trackable_items')
consolidated_df_cleaner3= explode(consolidated_df_cleaner2,'events')
consolidated_df_cleaner3.to_csv('/home/response4.csv',index=False)

expected output :

tracking_results   trackable_items   events
intransit           abc              22
intransit           xqy              23
Myccha

Try


(consolidated_df
    .pipe(explode,'tracking_results')
    .pipe(explode,'trackable_items')
    .pipe(explode,'events')
    .to_csv('/home/response4.csv',index=False)
)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python: Apply a custom function to multiple specified columns in a dataframe

How to apply a function that splits multiple numbers to the fields of a column in a dataframe in Python?

How to apply a custom filtering function on a Spark DataFrame

How to apply a custom function to groups in a dask dataframe, using multiple columns as function input

Apply custom function to dataframe

how to apply same function to multiple dataframes in R

how to apply same function to multiple divs

How to apply the same function to multiple ID's?

How to apply Window function to multiple columns in DataFrame

How to apply a function with multiple conditions to pandas dataframe?

How to apply a function with multiple arguments and create a dataframe?

How to tryCatch the same function call multiple times (N times) in R

How to get a dataframe with multiple rows based on custom function with apply or purrr functions?

How to apply a function to certain columns for multiple times in R?

How to apply multiple custom functions on multiple columns in grouped DataFrame in pandas?

How to apply multiple functions to same column in Python?

How to use custom component multiple times in same component

How to apply method/function to a dataframe in python

How to merge to same dataframe multiple times without the _x and _y suffixes?

How to run a function multiple times in python

Apply a custom function on a pandas dataframe

Apply custom function to entire dataframe

Python run one function multiple times at the same time

How can I return multiple rows from a python function to a pandas dataframe using apply?

How to apply a custom function to each column of my dataframe

How to apply custom function to 2 columns of a pandas dataframe?

How to apply custom function to two dataframe by cell / element?

How to use same onclick function multiple times in php page?

How to call the same function multiple times by using different ids