[英]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. 我是Python和Pandas的新手,我试图从Series中获取一些统计信息。 I want to calculate the difference between the last row and the first row , and put that in a new Series.
我想计算最后一行和第一行之间的差异 ,并将其放入新的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. 但是,我在最后一行尝试附加信息,但出现
TypeError
。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.