Created
April 26, 2019 18:39
-
-
Save Sh4pe/cdf6feea28efbcd4b46d51a6a69a0cdc to your computer and use it in GitHub Desktop.
Reduce over DataFrames with the same index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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