Pandas系列:每次求一个月内的值之和
Pandas series: find the sum of values in a period of one month each time
假设我们有系列:
2015-12-19 00:00:00 0.02347
2015-12-19 01:00:00 0.02152
2015-12-19 02:00:00 0.02329
2015-12-19 03:00:00 0.02045
2015-12-19 04:00:00 0.01962
2015-12-19 05:00:00 0.01896
2015-12-19 06:00:00 0.02184
2015-12-19 07:00:00 0.0239
2015-12-19 08:00:00 0.02928
2015-12-19 09:00:00 0.03008
2015-12-19 10:00:00 0.03007
.
.
.
2016-08-18 23:00:00 0.02892
我想找到每个月的值总和,但不是从每个月的 1 号开始。我想找到例如值的总和:
从 19-12-2015 到 18-1-2016
从 19-1-2016 到 18-2-2016
等等
这个怎么样?
import pandas as pd
import io
data = """2015-12-19 00:00:00;0.02347
2015-12-19 01:00:00; 0.02152
2015-12-19 02:00:00; 0.02329
2015-12-19 03:00:00; 0.02045
2015-12-19 04:00:00; 0.01962
2015-12-19 05:00:00; 0.01896
2015-12-19 06:00:00; 0.02184
2015-12-19 07:00:00; 0.0239
2015-12-19 08:00:00; 0.02928
2016-12-19 09:00:00; 0.03008
2016-12-20 10:00:00; 0.03007"""
df = pd.read_csv(io.StringIO(data), sep=";", header=None, index_col=0, parse_dates=True)
# subtract 19 days
df.index = df.index - pd.Timedelta(days=19)
# sum months
df = df.groupby(pd.Grouper(freq='M')).sum()
# apply offsets again
df.index = df.index + pd.Timedelta(days=19)
假设我们有系列:
2015-12-19 00:00:00 0.02347
2015-12-19 01:00:00 0.02152
2015-12-19 02:00:00 0.02329
2015-12-19 03:00:00 0.02045
2015-12-19 04:00:00 0.01962
2015-12-19 05:00:00 0.01896
2015-12-19 06:00:00 0.02184
2015-12-19 07:00:00 0.0239
2015-12-19 08:00:00 0.02928
2015-12-19 09:00:00 0.03008
2015-12-19 10:00:00 0.03007
.
.
.
2016-08-18 23:00:00 0.02892
我想找到每个月的值总和,但不是从每个月的 1 号开始。我想找到例如值的总和:
从 19-12-2015 到 18-1-2016
从 19-1-2016 到 18-2-2016
等等
这个怎么样?
import pandas as pd
import io
data = """2015-12-19 00:00:00;0.02347
2015-12-19 01:00:00; 0.02152
2015-12-19 02:00:00; 0.02329
2015-12-19 03:00:00; 0.02045
2015-12-19 04:00:00; 0.01962
2015-12-19 05:00:00; 0.01896
2015-12-19 06:00:00; 0.02184
2015-12-19 07:00:00; 0.0239
2015-12-19 08:00:00; 0.02928
2016-12-19 09:00:00; 0.03008
2016-12-20 10:00:00; 0.03007"""
df = pd.read_csv(io.StringIO(data), sep=";", header=None, index_col=0, parse_dates=True)
# subtract 19 days
df.index = df.index - pd.Timedelta(days=19)
# sum months
df = df.groupby(pd.Grouper(freq='M')).sum()
# apply offsets again
df.index = df.index + pd.Timedelta(days=19)