Skip to content
Advertisement

reduce() to merge if there are blank DataFrame

I want to use reduce() function to merge data.

final = reduce(lambda left,right: pd.merge(left,right,on='KEY',how="outer"), [df1, df2, df3, df4, df5, df6, df7, df8])

However, sometimes some dataframe df1 to df8 might be blank (but there is at least one dataframe not be blank).
And I do not want to detect which one.

For example, this time df1 to df7 are blank and only df8 is non-blank. Next time df1, df2, df5 are non-blank.

How should I do so?

Advertisement

Answer

You can rewrite your function to check for blank dataframes using the property DataFrame.empty:

def my_merge(left,right):
    if left.empty: return right
    if right.empty: return left
    return pd.merge(left,right)

final = reduce(my_merge, list_of_dfs)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement