Home > Software design >  Best way to create pivot tables for multiple dataframes?
Best way to create pivot tables for multiple dataframes?

Time:10-23

I have 4 data frames and trying to create pivot tables for each data frame. Instead of combining or concatenating into one large table, I'd like to create a pivot table for each dataframe, which means I would end up with 4 separate pivot tables. I am trying to create a loop and pass it through each data frame. Can someone help me modify the below code?

df_list = [df1, df2,df3, df4]

def fiftieth(x):
    return x.median()
def sixtieth(x):
    return x.quantile(0.60)
def seventieth(x):
    return x.quantile(0.70)
def seventy_fifth(x):
    return x.quantile(0.75)
def eightieth(x):
    return x.quantile(0.90)
def ninetieth(x):
    return x.quantile(0.90)       

for df in df_list: 
 
    pd.pivot_table([], values='Score', index='Measure_ID',
                       aggfunc=[fiftieth,sixtieth,seventieth,
                                 seventy_fifth,eightieth,ninetieth],dropna=True)

CodePudding user response:

As said in comments, you can use:

out = []
for df in df_list:
    out.append(df.pivot_table(values='Score', index='Measure_ID',
                              aggfunc=[fiftieth,sixtieth,seventieth,
                                       seventy_fifth,eightieth,ninetieth],
                              dropna=True))
  • Related