- The following function will work if there are only two
lists
of dataframes
.
- The
lists
of dataframes
are passed to the function as *args
, so any number of lists
can be passed to the function.
- The type annotations indicate the
*args
are a list
of dataframes
, and a tuple
of dataframes
is returned by the function.
list(zip(*v))
is used to create the correct groups of dataframes
for pandas.concat
.
- The number of tables,
t#
, returned by the function corresponds to the number of dataframes
in the lists
.
- If the
lists
contain more than the number of dataframes shown (e.g. df5
, df5_
and df5__
, then add a line of code to the function for t5
and return
it.
import pandas as pd
from typing import List, Tuple # for type annotations
# function to create report tables
def my_func(*v: List[pd.DataFrame]) -> Tuple[pd.DataFrame]:
l = list(zip(*v)) # use zip to combine the dataframes into the correct groups
t1 = l[0][0].round(2) # l[0] = (df1, df1_, df1__) → l[0][0] = df1
t2 = pd.concat(l[1]).round(2) # l[1] = (df2, df2_, df2__)
t3 = pd.concat(l[2]).round(2) # l[2] = (df3, df3_, df3__)
t4 = pd.concat(l[3]).applymap('{:.2%}'.format) # l[3] = (df4, df4_, df4__)
return t1, t2, t3, t4
# data
l1 = [df1, df2, df3, df4]
l2 = [df1_, df2_, df3_, df4_]
l3 = [df1__, df2__, df3__, df4__]
# function call with 3 lists
rt1, rt2, rt3, rt4 = my_func(l1, l2, l3)
# function call with 2 lists
rt1, rt2, rt3, rt4 = my_func(l1, l2)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…