Skip to content

Instantly share code, notes, and snippets.

@Sh4pe
Created April 26, 2019 18:39
Show Gist options
  • Save Sh4pe/cdf6feea28efbcd4b46d51a6a69a0cdc to your computer and use it in GitHub Desktop.
Save Sh4pe/cdf6feea28efbcd4b46d51a6a69a0cdc to your computer and use it in GitHub Desktop.
Reduce over DataFrames with the same index
import numpy.random as npr
import pandas as pd
from functools import reduce
def get_dfs():
for i in range(10):
yield pd.DataFrame(index=npr.permutation(10), data={f'c_{i}': npr.rand(10)})
def reducer(acc, x):
merged = acc.merge(x, left_index=True, right_index=True, indicator=True, how='outer')
assert 0 == merged[merged['_merge'] != 'both'].shape[0]
return merged.drop('_merge', axis=1)
reduce(reducer, get_dfs())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment