Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas get the row-wise minimum value of two or more columns

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']) 

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 
like image 375
kilojoules Avatar asked Nov 28 '15 18:11

kilojoules


People also ask

How do I get the minimum of two columns in pandas?

Min value between two pandas columns You can do so by using the pandas min() function twice.

How do you find the minimum value in a row in Python?

Use min() function to find the minimum value over the index axis. 2) Get minimum values of every row : Use min() function on a dataframe with 'axis = 1' attribute to find the minimum value over the row axis.

How do you find the minimum value in pandas?

Pandas DataFrame min() MethodThe min() method returns a Series with the minimum value of each column. By specifying the column axis ( axis='columns' ), the max() method searches column-wise and returns the minimum value for each row.

Is pandas query faster than LOC?

The query function seams more efficient than the loc function. DF2: 2K records x 6 columns. The loc function seams much more efficient than the query function.


1 Answers

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 
like image 147
Happy001 Avatar answered Sep 19 '22 23:09

Happy001