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
155 views
in Technique[技术] by (71.8m points)

python - Apply a function to a list of Pandas dfs

I have a reoccurring task in the code I am developing where I have a list of functions such as:

df1 = pd.DataFrame(data=[1,2,3], columns=['a', 'b', 'c'])
df2 = pd.DataFrame(data=[4,5,6], columns=['a', 'b', 'c'])
df3 = pd.DataFrame(data=[9,8,7], columns=['a', 'b', 'c'])
df_list = [df1, df2, df3]

And I want to apply a function (or filter) each one the same way. I am currently using a for loop to perform this task such as:

new_df_list = []
 for df in df_list:
    new_df_list.append(df.apply(some_func))

But I was wondering if there was a more elegant way that I can implement?

Thanks for any thoughts in advance!

question from:https://stackoverflow.com/questions/65599439/apply-a-function-to-a-list-of-pandas-dfs

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

1 Reply

0 votes
by (71.8m points)

Since you are looking for a more elegant way than a for loop, just store list of dataframes in a series object, apply your function using lambda x: x.apply(f) and convert back to a list. (NOTE: this is considering that your function doesn't directly take a data frame as input).

# The original function
def f(x):
    return x**2

#Apply using lambda function on each df
#Apply lambda function on each element of the series object
out = pd.Series(df_list).apply(lambda x: x.apply(f))

print(out[0])
   a  b  c
0  1  4  9

You can get away with a single apply if the function takes a data frame directly.

# With a function can take a DATAFRAME input
def f(df):
    return df**2

#Apply the apply(f) to each dataframe
out = pd.Series(df_list).apply(f)
   a  b  c
0  1  4  9

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

...