-1

I'm fairly new to Python and Pandas, I'm trying to pull some statistics from a Series. I want to calculate the difference between the last row and the first row, and put that in a new Series.

My first series looks similar to this:

#    Symbol(1)  Symbol(2)  Symbol(3)
Mon  5          10         15
Tue  6          9          12
Wed  3          11         15

I would like to know how to easily create this resultant Series:

#    Symbol(1)  Symbol(2)  Symbol(3)
Diff -2         1          0

This is the latest iteration of the code I tried:

diffy = pd.concat([inputs.head(1),inputs.tail(1)])
diffy.dropna(axis='columns', inplace='true')

a = pd.Series(index=diffy.index)
for c in diffy.columns:
    a.append(pd.Series(data=[diffy[c][1]-diffy[c][0]], index=c))

However, I get a TypeError on the last line, where I try to append the information.

This question seems to be a very similar issue, but the accepted answer doesn't quite provide the full details.

Community
  • 1
  • 1
Nathan Goings
  • 1,145
  • 1
  • 15
  • 33

1 Answers1

3
diffy = inputs.iloc[-1, :] - inputs.iloc[0, :] # pandas Series

and you can do

inputs = inputs.append(diffy, ignore_index=True)
su79eu7k
  • 7,031
  • 3
  • 34
  • 40
  • Well I knew it was something easy. I had tried `inputs.iloc[1:] - inputs.iloc[:1]` based on the referenced question/answer so I was close! – Nathan Goings Mar 08 '16 at 15:26