Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 每个月的列值之和_Python_List_Pandas_Dataframe_Timestamp - Fatal编程技术网

Python 每个月的列值之和

Python 每个月的列值之和,python,list,pandas,dataframe,timestamp,Python,List,Pandas,Dataframe,Timestamp,我有这样一个数据帧: Timestamp Consumption 4/1/2017 20:00 257 4/1/2017 21:00 262 4/1/2017 22:00 256 4/1/2017 23:00 256 4/2/2017 0:00 263 4/2/2017 1:00 256 4/2/2017 2:00 265 4/2/2017 3:00 259 4/2/2017 4:00 256 4/2/2017 5:00 260 4/2/2017 6:00

我有这样一个数据帧:

Timestamp   Consumption
4/1/2017 20:00  257
4/1/2017 21:00  262
4/1/2017 22:00  256
4/1/2017 23:00  256
4/2/2017 0:00   263
4/2/2017 1:00   256
4/2/2017 2:00   265
4/2/2017 3:00   259
4/2/2017 4:00   256
4/2/2017 5:00   260
4/2/2017 6:00   265
4/2/2017 7:00   265
我想计算每个月的消费总额列,并将其放入列表中。比如:

[10312089]

并根据时间和日期进行计算。如23:00至06:00:

[2080]

我怎样才能做到这一点?请帮忙

第一个转换列:

然后以月为单位,用
求和

a = df.resample('m', on='Timestamp')['Consumption'].sum().dropna().tolist()
print (a)
[1031, 2089]
a = df.set_index('Timestamp')
      .groupby(pd.Grouper(freq='m'))['Consumption']
      .sum()
      .dropna()
      .tolist()
print (a)
[1031, 2089]
另一个类似的解决方案-添加:

使用
groupby
sum
的解决方案:

a = df.resample('m', on='Timestamp')['Consumption'].sum().dropna().tolist()
print (a)
[1031, 2089]
a = df.set_index('Timestamp')
      .groupby(pd.Grouper(freq='m'))['Consumption']
      .sum()
      .dropna()
      .tolist()
print (a)
[1031, 2089]
编辑:

如果在
时间戳
列中筛选了日期间,请使用:

编辑:

如有需要:



@杰克斯雷尔,如果我想根据时间和日期进行计算,比如23:00到06:00?你认为2017年4月2日<代码>和时间<代码>23:00到06:00<代码>?我的意思是2017年4月1日23:00到2017年4月2日06:00总是在时间戳<代码>列中的日期之间?或者有时不?我添加了新的数据样本,这是您需要的吗?
df.Timestamp = pd.to_datetime(df.Timestamp, dayfirst=True)
date1 = '2017-01-04 23:00'
date2 ='2018-02-04 06:00'
df1 = df.set_index('Timestamp')['Consumption']
a = df1.loc[date1:date2].sum()
print (a)
2080 
print (df)
         Timestamp  Consumption
0   4/1/2017 20:00          257
1   4/1/2017 21:00          262
2   4/1/2017 22:00          256
3   4/1/2017 23:00          256
4    4/2/2017 0:00          263
5    4/2/2017 1:00          256
6    4/2/2017 2:00          265
7    4/2/2017 3:00          259
8    4/2/2017 4:00          256
9    4/2/2017 5:00          260
10   4/2/2018 6:00          265
11   4/2/2018 7:00          265
12  4/3/2017 20:00          256
13  4/3/2017 21:00          263
14   4/3/2017 1:00          256
15   4/4/2017 2:00          265
16   4/4/2017 3:00          259
17   4/4/2017 8:00          256
df.Timestamp = pd.to_datetime(df.Timestamp, dayfirst=True)
df1 = df.set_index('Timestamp')['Consumption'].between_time('23:00','6:00')
print (df1)
Timestamp
2017-01-04 23:00:00    256
2017-02-04 00:00:00    263
2017-02-04 01:00:00    256
2017-02-04 02:00:00    265
2017-02-04 03:00:00    259
2017-02-04 04:00:00    256
2017-02-04 05:00:00    260
2018-02-04 06:00:00    265
2017-03-04 01:00:00    256
2017-04-04 02:00:00    265
2017-04-04 03:00:00    259
Name: Consumption, dtype: int64

print (df1.sum())
2860