0

I am using pandas to process a dataset (see samples below), where t and tu are timestamp (epoch in seconds and microsecond respectively). and br and bw are cumulative values at the sample time. I'd like to perform two actions:

  • split groups based A and B (there maybe many)
  • for each group, I'd like to compute the delta of br and bw, e.g. br[n] - br[n-1]

I am having difficulty to figure this out, any help is appreciated.

 # data as dictionary

 data = {0: {'group': 'A', 't': 1532973555, 'tu': 319007, 'br': 28, 'bw': 32},
 1: {'group': 'B', 't': 1532973555, 'tu': 319638, 'br': 100, 'bw': 200},
 2: {'group': 'A', 't': 1532973999, 'tu': 320594, 'br': 75, 'bw': 86},
 3: {'group': 'B', 't': 1532973999, 'tu': 320652, 'br': 300, 'bw': 500}}

# read into dataframe

df = pd.DataFrame.from_dict(data, orient="index")
python152
  • 1,811
  • 2
  • 15
  • 18

1 Answers1

0

To compute differences in a dataframe, you can use the diff method. In you case it would be something like that:

df.groupby('group')['br'].diff() 

0      NaN
1      NaN
2     47.0
3    200.0
Name: br, dtype: float64
ysearka
  • 3,805
  • 5
  • 20
  • 41
  • is there a way to keep group info? i.e. #2 (47) output is from group A, #3 (200) is from group B ... this seems necessary if extra steps are to be performed on the diff. thx – python152 Aug 16 '18 at 16:19
  • If you assign the Series created this way to a new column of your dataframe, it will be appended so that you will keep all the informations present in your dataframe. – ysearka Aug 17 '18 at 08:44