Applying same function on multiple pandas dataframe to get dataframes

Viewed 1379

I am trying to apply the same function to multiple pandas dataframe but the resultant is an object while I need dataframe as the result. I also tried using 'broadcast' but that didn't help much

Following is the function which basically assigns a name to the index column:

def assign_name(name_df):
  name_df.index.name="Fields"

& I am applying it to the uniques & skewness dataframes which basically consists of the number of unique values for each field & skewness:

uniques,skewness = (df.apply(assign_name)for df in [uniques,skewness])

The data type of uniques & skewness is object:

dtype: object

Can any one help me out in solving this?

3 Answers
def assign_name(x):
  x.index.name="Fields"
  return x

ll = [type(x) for x in [assign_name(df) for df in [df1, df2]]
print(ll)

Use pipe and make sure your function uses return:

def assign_name(name_df):
    name_df.index.name="Fields"
    return df

uniques, skewness = (df.pipe(assign_name)for df in [uniques,skewness])

The function remains the same just added a return: def assign_name(name_df): name_df.index.name="Fields" return(name_df) Storing all the dataframes as list & applying the function on the list:
list_df = [uniques, skewness,df1_transposed,coltypes] list_df = [df.pipe(assign_name) for df in list_df] Access each data frame as if you are accessing elements of a list: list_df[3]

Related