[英]Operations on Pandas DataFrame Index
How can I easily perform an operation on a Pandas DataFrame Index? 如何轻松对Pandas DataFrame Index执行操作? Lets say I create a DataFrame like so: 可以说我像这样创建一个DataFrame:
df = DataFrame(rand(5,3), index=[0, 1, 2, 4, 5])
and I want to find the mean sampling rate. 我想找到平均采样率。 The way I do this now doesn't seem quite right. 我现在这样做的方式似乎不太正确。
fs = 1./np.mean(np.diff(df.index.values.astype(np.float)))
I feel like there must be a better way to do this, but I can't figure it out. 我觉得必须有更好的方法来做到这一点,但我无法弄清楚。
Thanks for any help. 谢谢你的帮助。
@BrenBarn is correct, better to make a column in a frame, but you can do this @BrenBarn是正确的,最好在框架中创建一列,但是您可以这样做
In [2]: df = DataFrame(np.random.rand(5,3), index=[0, 1, 2, 4, 5])
In [3]: df.index.to_series()
Out[3]:
0 0
1 1
2 2
4 4
5 5
dtype: int64
In [4]: s = df.index.to_series()
In [5]: 1./s.diff().mean()
Out[5]: 0.80000000000000004
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.