Python 熊猫获得两列的最小值作为等式的一部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33975128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:16:05  来源:igfitidea点击:

pandas get minimum value of two columns as part of an equation

pythonpandasdataframe

提问by kilojoules

How can I reference the minimum value of two dataframes as part of a pandas dataframe equation? I tried using the python min()function which did not work. I'm sorry if this is well-documented somewhere but I have not been able to find a working solution for this problem. I am looking for something along the lines of this:

如何将两个数据帧的最小值作为熊猫数据帧方程的一部分?我尝试使用min()不起作用的python函数。如果这在某处有详细记录,我很抱歉,但我无法找到解决此问题的有效解决方案。我正在寻找类似的东西:

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

I also tried to use pandas min()function, which is also not working.

我也尝试使用 pandasmin()函数,这也不起作用。

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

InvalidIndexError: Reindexing only valid with uniquely valued Index objects

I was confused by this error. The data columns are just numbers and a name, I wasn't sure where the index comes into play.

我对这个错误感到困惑。数据列只是数字和名称,我不确定索引在哪里起作用。

In [108]: data['flow_c']
Out[108]: 
0       74.014640
1       74.150579
2       74.014640
3       73.960195
4       74.069046
5       73.960195
6       73.987423
7       73.905710

采纳答案by Happy001

Your question is not very clear to me, but my guess is that you are trying to get element-wise mininumof two Series(not DataFrames). If that's what you want, try: data[['flow_h','flow_c']].min(axis=1).

你的问题对我来说不是很清楚,但我的猜测是你正试图获得mininum两个Series(不是DataFrames)的元素。如果这是你想要的东西,尝试:data[['flow_h','flow_c']].min(axis=1)

回答by b0lle b

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.

第一个“min()”计算每列的最小值并返回一个熊猫系列。第二个“min”返回每列最小值中的最小值。