Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
83 views
in Technique[技术] by (71.8m points)

python - Create a function for a number of lists and correctly group the list elements

I have 3 lists, but sometimes only 2, that each contain 4 multi-index dataframes.

list1=[df1, df2, df3, df4]
list2=[df1_, df2_, df3_, df4_]
list3=[df1__, df2__, df3__, df4__]

The next step is to create multi-index dataframes:

reportTable1 = list1[0].round(2) #this dataframe is equal to list1[0], In other words "df1".
reportTable2 = pd.concat([list1[1], list2[1], list3[1]], axis = 0).round(2) #these dataframes have different columns.
reportTable3 = pd.concat([list1[2], list2[2], list3[2]], axis = 0).round(2) #they have same columns.
reportTable4 = pd.concat([list1[3], list2[3], list3[3]], axis = 0).applymap('{:.2%}'.format) #they have same columns.

Firstly, I want to define a function for these steps with cleaner code.

My main problem is that in some cases list3 doesn't exit. In this situation, I do not want to get error. How can I run this code in cases where list3 is not available?

question from:https://stackoverflow.com/questions/65647405/create-a-function-for-a-number-of-lists-and-correctly-group-the-list-elements

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • 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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...