8

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?

jpp
  • 159,742
  • 34
  • 281
  • 339
Naravut Suvannang
  • 187
  • 1
  • 4
  • 11

2 Answers2

8

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.

jpp
  • 159,742
  • 34
  • 281
  • 339
6

If the data frames have the same columns you could concat them to a single data frame, but otherwise there is not really a "smart" way of doing it:

df1, df2, df3 = (df.apply(...) for df in [df1, df2, df3]) # or either .map or .applymap
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • Isn't the method name applymap (instead of mapapply)? I often forget myself, but my IDEs rarely ;-) – Dilettant Jul 07 '16 at 11:19
  • I can't 'concat' data frames because they are difference type of data but all data frames need to do exactly the same workflow. – Naravut Suvannang Jul 08 '16 at 12:40
  • It do not work, so is anything wrong 'def unique(df): df['bioactivity_type'].unique() df1, df2, df3 = (df.apply(unique) for df in [df1, df2, df3])' KeyError: ('bioactivity_type', u'occurred at index activity_comment') – Naravut Suvannang Jul 09 '16 at 06:37