mardi 14 novembre 2017

Aggregate n DataFrames into a single DataFrame - element-wise

What is the pythonic way to generate a pandas DataFrame using an aggregation function to combine several other DataFrames? (DataFrames are all of the same shape with same headers.)

Aggregation functions I might be interested in are mean, minimum, maximum and user defined functions taking two operands.

For example, this is the way I might generate a DataFrame (max) which has the maximum element from four other DataFrames (df1,df2,df3,df4). This is hardly a pattern (idiom) that I can apply in all situations and to calculate the mean would look quite different.

import pandas as pd

df1 = pd.DataFrame({'a' : [1,2,3], 'b': [3,9,5]})
df2 = pd.DataFrame({'a' : [6,2,7], 'b': [3,4,5]})
df3 = pd.DataFrame({'a' : [6,2,11], 'b': [3,4,5]})
df4 = pd.DataFrame({'a' : [6,2,7], 'b': [3,12,5]})
print(df1)
print(df2)
print(df3)
print(df4)

max1 = df1.where(df1 > df2, df2)
max2 = df3.where(df3 > df4, df4)
max = max1.where(max1 > max2, max2)
print(max)

Aucun commentaire:

Enregistrer un commentaire