简体   繁体   中英

Python Pandas, creating new series from calculations on existing series

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.

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

你可以做

inputs = inputs.append(diffy, ignore_index=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM