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

认为,

  worksheet 1

    index    A   B   C
       0     2   3   4.28
       1     3   4   5.23
    worksheet 2

    index    A   B   C
        0    9   6   5.9
        1    1   3   4.1

    worksheet 3

    index    A   B   C
        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, [w.C 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.concat 并将 pd.Series.minlevel=1 参数一起使用
pd.concat(dict(enumerate([w.C for w in [ws1, ws2, ws3]]))).min(level=1)
# equivalently
# pd.concat(dict(enumerate([w.C for w in [ws1, ws2, ws3]])), axis=1).min(1)

index
0    4.28
1    4.10
Name: C, dtype: float64

注意:
dict(enumerate([w.C for w in [ws1, ws2, ws3]]))

是另一种说法
{0: ws1.C, 1: ws2.C, 2: ws3.C}

关于python - 使用 Pandas 在多个工作表中查找最小值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44772747/

10-10 17:58