使用pandas在多个工作表中查找最小值

如何在整个工作表中为每个索引find多个工作表中的最小值

假设,

worksheet 1 index ABC 0 2 3 4.28 1 3 4 5.23 worksheet 2 index ABC 0 9 6 5.9 1 1 3 4.1 worksheet 3 index ABC 0 9 6 6.0 1 1 3 4.3 ...................(Worksheet 4,Worksheet 5)........... by comparing C column, I want an answer, where dataframe looks like index min(c) 0 4.28 1 4.1 

 from functools import reduce reduce(np.fmin, [ws1.C, ws2.C, ws3.C]) index 0 4.28 1 4.10 Name: C, dtype: float64 

这很好地概括了理解

 reduce(np.fmin, [wC for w in [ws1, ws2, ws3, ws4, ws5]]) 

如果你必须坚持你的专栏名称

 from functools import reduce reduce(np.fmin, [ws1.C, ws2.C, ws3.C]).to_frame('min(C)') min(C) index 0 4.28 1 4.10 

您也可以在字典中使用pd.Series.min ,并使用level=1参数的pd.Series.min

 pd.concat(dict(enumerate([wC for w in [ws1, ws2, ws3]]))).min(level=1) # equivalently # pd.concat(dict(enumerate([wC for w in [ws1, ws2, ws3]])), axis=1).min(1) index 0 4.28 1 4.10 Name: C, dtype: float64 

注意:

 dict(enumerate([wC for w in [ws1, ws2, ws3]])) 

是另一种说法

 {0: ws1.C, 1: ws2.C, 2: ws3.C} 

您需要参数sheetname=None read_excel从所有工作表名称的OrderedDict s然后用numpy.fmin reduce列表理解:

 dfs = pd.read_excel('file.xlsx', sheetname=None) print (dfs) OrderedDict([('Sheet1', ABC 0 2 3 4.28 1 3 4 5.23), ('Sheet2', ABC 0 9 6 5.9 1 1 3 4.1), ('Sheet3', ABC 0 9 6 6.0 1 1 3 4.3)]) from functools import reduce df = reduce(np.fmin, [v['C'] for k,v in dfs.items()]) print (df) 0 4.28 1 4.10 Name: C, dtype: float64 

concat解决scheme:

 df = pd.concat([v['C'] for k,v in dfs.items()],axis=1).min(axis=1) print (df) 0 4.28 1 4.10 dtype: float64 

如果需要在read_excel定义索引:

 dfs = pd.read_excel('file.xlsx', sheetname=None, index_col='index') print (dfs) OrderedDict([('Sheet1', ABC index 0 2 3 4.28 1 3 4 5.23), ('Sheet2', ABC index 0 9 6 5.9 1 1 3 4.1), ('Sheet3', ABC index 0 9 6 6.0 1 1 3 4.3)]) df = pd.concat([v['C'] for k,v in dfs.items()], axis=1).min(axis=1) print (df) index 0 4.28 1 4.10 dtype: float64