熊猫获得两列或多列的行最小值

如何引用两个数据框架的最小值作为熊猫数据框架方程的一部分?我尝试使用 pythonmin()函数,但是没有用。我很抱歉,如果这是很好的文件的地方,但我没有能够找到一个工作的解决方案,这个问题。我正在寻找类似这样的东西:

data['eff'] = pd.DataFrame([data['flow_h'], data['flow_c']]).min() *Cp* (data[' Thi'] - data[' Tci'])

我还尝试使用熊猫 min()函数,这也是不工作的。

min_flow = pd.DataFrame([data['flow_h'], data['flow_c']]).min()


InvalidIndexError: Reindexing only valid with uniquely valued Index objects

我被这个错误弄糊涂了。数据列只是数字和名称,我不确定索引在哪里起作用。

import pandas as pd
import numpy as np


np.random.seed(365)
rows = 10
flow = {'flow_c': [np.random.randint(100) for _ in range(rows)],
'flow_d': [np.random.randint(100) for _ in range(rows)],
'flow_h': [np.random.randint(100) for _ in range(rows)]}
data = pd.DataFrame(flow)


# display(data)
flow_c  flow_d  flow_h
0      82      36      43
1      52      48      12
2      33      28      77
3      91      99      11
4      44      95      27
5       5      94      64
6      98       3      88
7      73      39      92
8      26      39      62
9      56      74      50
113740 次浏览

If you are trying to get the row-wise mininum of two or more columns, use pandas.DataFrame.min. Note that by default axis=0; specifying axis=1 is necessary.

data['min_c_h'] = data[['flow_h','flow_c']].min(axis=1)


# display(data)
flow_c  flow_d  flow_h  min_c_h
0      82      36      43       43
1      52      48      12       12
2      33      28      77       33
3      91      99      11       11
4      44      95      27       27
5       5      94      64        5
6      98       3      88       88
7      73      39      92       73
8      26      39      62       26
9      56      74      50       50

If you like to get a single minimum value of multiple columns:

data[['flow_h','flow_c']].min().min()

the first "min()" calculates the minimum per column and returns a pandas series. The second "min" returns the minimum of the minimums per column.