Home > other >  panda cumulative subtraction
panda cumulative subtraction

Time:12-07

I have a column in the dataframe called Adjusted Feed Vessel Weight, where Adjusted Feed Vessel Weight= [5000, 4000, 3000, 2000, 1000], for example. I am trying to take the cum. difference so it looks like [0, 1000, 2000, 3000, 4000].

when i tried to use the code below, it gave me an error "ValueError: Cannot set a DataFrame with multiple columns to the single column feed_difference".

When i print "feed_weight", its not in a single column. So instead, it look like this:

Adjusted Feed Vessel Weight     0      1      2      3     4     5     6  
hermes_run                                                                 
H11958-5                     39.2  100.8  111.3  101.5  84.0  92.4  91.7 

How can I get it to read in a single column?

feed_weight = RCS_df.groupby('hermes_run').apply(lambda x: x['Adjusted Feed Vessel Weight'].sub(x['Adjusted Feed Vessel Weight'].shift(-1))) # g
#RCS_df['Weight_draw'] = Weight_draw.reset_index(level = 0, drop=True) # kg
feed_difference = feed_weight.shift(1)
feed_difference = feed_difference.fillna(0)
RCS_df['feed_difference'] = feed_difference.reset_index(level = 0, drop=True) # g

CodePudding user response:

Your input is unclear, so based on this example:

df = pd.DataFrame({'Adjusted Feed Vessel Weight': [5000, 4000, 3000, 2000, 1000]})

You can compute the diff, then cumsum:

df['cumsum'] = (-df['Adjusted Feed Vessel Weight'].diff()).fillna(0).cumsum()

Output:

   Adjusted Feed Vessel Weight  cumsum
0                         5000     0.0
1                         4000  1000.0
2                         3000  2000.0
3                         2000  3000.0
4                         1000  4000.0

CodePudding user response:

I think this should work:

df['feed_difference'] = df['Adjusted Feed Vessel Weight'].iat[0] - df['Adjusted Feed Vessel Weight']
  • Related