Skip to content Skip to sidebar Skip to footer

Python Pandas-possible To Compare 3 Dfs Of Same Shape Using Where(max())? Is This A Masking Issue?

I have a dict containing 3 dataframes of identical shape. I would like to create: a 4th dataframe which identifies the largest value from the original 3 at each coordinate - so di

Solution 1:

consider the dictdfs which is a dictionary of pd.DataFrames

import pandas as pd
import numpy as np

np.random.seed([3,1415])
dfs = dict(
    one=pd.DataFrame(np.random.randint(1, 10, (5, 5))),
    two=pd.DataFrame(np.random.randint(1, 10, (5, 5))),
    three=pd.DataFrame(np.random.randint(1, 10, (5, 5))),
)

the best way to handle this is with a pd.Panel object, which is the higher dimensional object analogous to pd.DataFrame.

p = pd.Panel(dfs)

then the answers you need are very straighforward

maxp.max(axis='items') or p.max(0)

penultimatep.apply(lambda x: np.sort(x)[-2], axis=0)

Solution 2:

The 1st question is easy to answer, you could use the numpy.maximum() function to find the element wise maximum value in each cell, across multiple dataframes

dic ['four'] = pd.DataFrame(np.maximum(dic['one'].values,dic['two'].values,dic['three'].values),columns = list('ABC'))

Post a Comment for "Python Pandas-possible To Compare 3 Dfs Of Same Shape Using Where(max())? Is This A Masking Issue?"