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

python - How to apply function to multiple pandas dataframe

I have multiple dataframes:

df1, df2, df3,..., dfn

They have the same type of data but from different groups of descriptors that cannot be joined. Now I need to apply the same function to each dataframe manually.

How can I apply the same function to multiple dataframes?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

pipe + comprehension

If your dataframes contain related data, as in this case, you should store them in a list (if numeric ordering is sufficient) or dict (if you need to provide custom labels to each dataframe). Then you can pipe each dataframe through a function foo via a comprehension.

List example

df_list = [df1, df2, df3]
df_list = [df.pipe(foo) for df in df_list]

Then access your dataframes via df_list[0], df_list[1], etc.

Dictionary example

df_dict = {'first': df1, 'second': df2, 'third': df3}
df_dict = {k: v.pipe(foo) for k, v in df_dict.items()}

Then access your dataframes via df_dict['first], df_dict['second'], etc.


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

...