Subtract the minimum value from the maximum value across each row, Python Pandas DataFrame

pandas subtract series from dataframe
pandas subtract two rows
pandas subtract one column from multiple columns
pandas subtract two columns
subtract the mean of a row from each element of the row in a data frame
pandas subtract two columns from different dataframes
pandas min
pandas max of two columns

I have a census dataset indexed by state name and county name and want to loop through each row to find the max and min value across all columns labelled as 'population estimate in each year', then subtract these two values. I want the function to return a Pandas Series with index and value.

Here is my current code:

columns_to_keep=[
    'STNAME',
    'CTYNAME',
    'POPESTIMATE2010',
    'POPESTIMATE2011',
    'POPESTIMATE2012',
    'POPESTIMATE2013',
    'POPESTIMATE2014',
    'POPESTIMATE2015' 
]
df=census_df[columns_to_keep]

def answer_seven(lst):
    lst=[df['POPESTIMATE2010'],df['POPESTIMATE2011'],df['POPESTIMATE2012'],
             df['POPESTIMATE2013'],df['POPESTIMATE2014'],df['POPESTIMATE2015']]

    return max(lst)-min(lst)

answer_seven(lst)

error message:

ValueError                                Traceback (most recent call last)
<ipython-input-110-845350b0b5f7> in <module>()
     18     return max(lst)-min(lst)
     19 
---> 20 answer_seven(lst)
     21 

<ipython-input-110-845350b0b5f7> in answer_seven(lst)
     16              df['POPESTIMATE2013'],df['POPESTIMATE2014'],df['POPESTIMATE2015']]
     17 
---> 18     return max(lst)-min(lst)
     19 
     20 answer_seven(lst)

/opt/conda/lib/python3.5/site-packages/pandas/core/generic.py in __nonzero__(self)
    890         raise ValueError("The truth value of a {0} is ambiguous. "
    891                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 892                          .format(self.__class__.__name__))
    893 
    894     __bool__ = __nonzero__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Or consider numpy.ptp for speed:

Range of values (maximum - minimum) along an axis.

np.ptp(df[cols_of_interest].values, axis=1)

Select row with maximum and minimum value in Pandas dataframe , Range of values (maximum - minimum) along an axis. Pandas can do this directly: The return of this will be a series indexed by the original index of your dataframe and the maximum value for each row minus the minimum value Browse other questions tagged python pandas dataframe max min or  It returned a series with column names as index label and minimum value of each column in values. Similarly we can find minimum values in every row too, Get minimum values of every row. To find minimum value of every row in DataFrame just call the min() member function with DataFrame object with argument axis=1 i.e.

Pandas can do this directly:

cols_of_interest = ['POPESTIMATE2010', 'POPESTIMATE2011', 'POPESTIMATE2012', 'POPESTIMATE2013', 'POPESTIMATE2014' , 'POPESTIMATE2015']
df[cols_of_interest].max(axis=1) - df[cols_of_interest].min(axis=1)

The return of this will be a series indexed by the original index of your dataframe and the maximum value for each row minus the minimum value

pandas.DataFrame.subtract, A Computer Science portal for geeks. Select row with maximum and minimum value in Pandas dataframe importing pandas and numpy Code #1: Shows max on Driver, Points, Age columns. to select multiple columns in a pandas dataframe · Python | Pandas DataFrame.fillna() to replace Null values in dataframe  Finding minimum and maximum values: import pandas as pd df = pd.DataFrame([[10, 20, 30, 40], [7, 14, 21, 28], [55, 15, 8, 12], [15, 14, 1, 8], [7, 1, 1, 8], [5, 4, 9

I had trouble with NaN values that I needed to keep and used the following:

x = {}
for col in df_count:
    x[col] = df_count[col].max()- df_count[col].min()
pd.Series(x)

The Data Science Workshop: A New, Interactive Approach to Learning , Broadcast across a level, matching Index values on the passed MultiIndex level. fill_valuefloat or None, default None. Fill existing missing (NaN) values, and any​  This is my dataframe df. a b c 1.2 2 0.1 2.1 1.1 3.2 0.2 1.9 8.8 3.3 7.8 0.12 I'm trying to get max value from each row of a dataframe, I m expecting output like this

pandas.DataFrame.abs, It does this by subtracting the mean of the data and dividing by the range, which is the minimum value subtracted from the maximum value. PolynomialFeatures will engineer new features by raising the values in a column up to a certain power and creating new columns in your DataFrame to accommodate them. 3. Read in  To find the minimum value in each row, you need to specify axis 1: >>> numbers.min(axis=1) array([ 0, 4, 8, 12, 16]) For a 2D array, numbers.min() finds the single minimum value in the array, numbers.min(axis=0) returns the minimum value for each column and numbers.min(axis=1) returns the minimum value for each row.

pandas.DataFrame.min, Return a Series/DataFrame with absolute numeric value of each element. Select rows with data closest to certain value using argsort (from StackOverflow). Return the minimum. Series.max. Return the maximum. Series.idxmin. Return the index of the minimum. Series.idxmax. Return the index of the maximum. DataFrame.sum. Return the sum over the requested axis. DataFrame.min. Return the minimum over the requested axis. DataFrame.max. Return the maximum over the requested axis. DataFrame.idxmin

pandas.Series.max, Return the minimum of the values for the requested axis. a MultiIndex (​hierarchical), count along a particular level, collapsing into a Series. DataFrame.max. To find the maximum value of a Pandas DataFrame, you can use pandas.DataFrame.max() method. Using max(), you can find the maximum value along an axis: row wise or column wise, or maximum of the entire DataFrame. Example 1: Find Maximum of DataFrame along Columns. In this example, we will calculate the maximum along the columns.

Comments
  • Possible duplicate of Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
  • How to also return the index label using this code?
  • You could create a Series, pd.Series(np.ptp(df[cols_of_interest].values, axis=1), index=df.index).
  • I was able to find the county with the maximum population difference across the years, but dtype is int64. How do I have it return only the county name (index) as a string?