大 pandas 获得两列或更多列的按行最小值 [英] pandas get the row-wise minimum value of two or more columns

查看:79
本文介绍了大 pandas 获得两列或更多列的按行最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为熊猫数据框方程式的一部分,如何引用两个数据框的最小值?我尝试使用无法运行的python min()函数。很抱歉,如果某处对此进行了详细记录,但我无法找到解决此问题的可行解决方案。我正在寻找类似的东西:

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:

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

我还尝试过使用熊猫 min()函数,该函数也不起作用。

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

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.

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


推荐答案

如果要获取两列或更多列的行式最小值,请使用 pandas.DataFrame.min 并指定 axis = 1

If you are trying to get the row-wise mininum of two or more columns, use pandas.DataFrame.min and specify axis=1.

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

这篇关于大 pandas 获得两列或更多列的按行最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆