1

I'm trying to get the minimum increase between rows in a column in my table.

my attempt so far

import pandas as pd

df = pd.DataFrame({'A': [0, 100, 50, 100],
                   'B': [5, 2, 2, 0],
                   'C': [10, 20, 40, 400]})

def lowestIncrease(data, value):
  table = data[value].pct_change().fillna(0)
  x = data[value][table == table.min(i for i in table if i > 0)].index[0]
  if x == 0:
    print( 0 )
  else:
    answer = data[value][x] - data[value][x-1]
    print( answer )

My idea to use min() the way that that I am came from here https://stackoverflow.com/a/27966830/8705615

my desired output

in[1]:lowestIncrease(df, 'A')
out[1]:50
in[2]:lowestIncrease(df, 'B')
out[2]:0
in[3]:lowestIncrease(df, 'C')
out[3]:10

I feel like I'm close to what I'm trying to achieve, I just need to replace how I'm calling table.min(i for i in table if i > 0) to get my desired output. How do you get the lowest change in value above 0 in a table?

CSstudent
  • 669
  • 2
  • 7
  • 27

1 Answers1

5

Inputs:

df = pd.DataFrame({'A': [0, 100, 50, 100],
                   'B': [5, 2, 2, 0],
                   'C': [10, 20, 40, 400]})

Solution:

df.diff()[df.diff() >0].min().fillna(0)

Output

A    50.0
B     0.0
C    10.0

For a single column

d = df[['A']]
d.diff()[d.diff() >0].min().fillna(0)

Output

A    50.0
Yuca
  • 6,010
  • 3
  • 22
  • 42
  • my code crashes if I leave `.fillna(0)` in but if I remove it it works. but 'B' return nan – CSstudent Aug 24 '18 at 20:09
  • If I run exactly as your post it works, but I'm trying to have the code for individual columns so i changed the `df`'s to `df[column_name]`. It crashes if I run it like this, but it doesn't look like it should. – CSstudent Aug 24 '18 at 20:14
  • I had to rap the entire second line for single column in a `float()` for anyone who wants the value only – CSstudent Aug 24 '18 at 20:26
  • 1
    or you can add *.values[0]* – Yuca Aug 24 '18 at 20:30