[英]Pandas TimeGrouper by column
I have a csv file with dates as columns headers and binary a matrix of 1, 0 or np.nan
. 我有一个csv文件,其中日期作为列标题,二进制文件是1、0或
np.nan
的矩阵。
I'd like to take the mean of each index, grouped by month. 我想将每个索引的平均值按月分组。 I am running into a problem because my columns are not a datetimeindex, which I try to convert to with
pd.to_datetime()
with no luck. 我遇到了一个问题,因为我的列不是datetimeindex,我尝试用
pd.to_datetime()
转换为没有运气。
binary.csv: binary.csv:
2016-01-01 00:00:00,2016-01-02 00:00:00,2016-02-01 00:00:00,2016-02-02 00:00:00
1,,0,1
0,1,,1
My Code: 我的代码:
import pandas as pd
import numpy as np
df = pd.read_csv('binary.csv')
df.columns = pd.to_datetime(df.columns, format='%Y-%m-%d %H:%M:%S')
df = df.groupby(pd.TimeGrouper(freq='M'), axis=0)
print df
Error: 错误:
TypeError: axis must be a DatetimeIndex, but got an instance of 'Int64Index'
Desired Output: 所需输出:
2016-01-01 00:00:00 2016-02-01 00:00:00
0 1.0 0.5
1 0.5 1.0
Updated question: 更新的问题:
Based on best answer: 根据最佳答案:
If I wanted to a single value for each month, is there a more efficient way to do that than this? 如果我想每个月取一个值,是否有比这更有效的方法呢?
pd.DataFrame(data=df.resample('MS', axis=1).mean().mean()).transpose()
By default, pd.TimeGrouper works on the index (axis=0) so you need to tell it that it should group the columns instead: 默认情况下,pd.TimeGrouper在索引(axis = 0)上起作用,因此您需要告诉它应该对列进行分组:
df.groupby(pd.TimeGrouper(freq='MS', axis=1), axis=1).mean()
Out:
2016-01-01 2016-02-01
0 1.0 0.5
1 0.5 1.0
You can directly use resample, too: 您也可以直接使用重采样:
df.resample('MS', axis=1).mean()
Out:
2016-01-01 2016-02-01
0 1.0 0.5
1 0.5 1.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.