2017-08-26 80 views
1

我有每天的时间序列数据下面的数据框:获取大熊猫每列最大值的数量

time-orig 00:15:00 00:30:00 00:45:00 01:00:00 
date     
2010-01-04 1164.3 1163.5 1162.8 1161.8 
2010-01-05 1186.3 1185.8 1185.6 1185.0 
2010-01-06 1181.5 1181.5 1182.7 1182.3 
2010-01-07 1202.1 1201.9 1201.7 1200.8 

现在我想每列最大值的这样的数字:

'00:15:00' : 3 
'00:30:00' : 0 
'00:45:00' : 1 
'01:00:00' : 0 

(即:列'00:15:00'有3个最大值,每行最大值。)

我知道我可以转置数据帧并在列上运行循环并使用idxmax(),但我的问题是如果有一个矢量化/更好的方法来做到这一点?

回答

1

假设此处所作该date是索引。您可以使用df.idxmax其次df.value_counts

print(df) 
time-orig 00:15:00 00:30:00 00:45:00 01:00:00 
date            
2010-01-04 1164.3 1163.5 1162.8 1161.8 
2010-01-05 1186.3 1185.8 1185.6 1185.0 
2010-01-06 1181.5 1181.5 1182.7 1182.3 
2010-01-07 1202.1 1201.9 1201.7 1200.8 

s = df.idxmax(1).value_counts().reindex(df.columns, fill_value=0) 
print(s) 

time-orig 
00:15:00 3 
00:30:00 0 
00:45:00 1 
01:00:00 0 
dtype: int64 

Divakar的解决方案是,如果你想有一个numpy的阵列相当快。为了您的确切数据,有轻微的修改是需要他的回答:

val = np.bincount(df.values.argmax(1), minlength=df.shape[1]) 
s = pd.Series(val, df.columns) 
print(s) 

time-orig 
00:15:00 3 
00:30:00 0 
00:45:00 1 
01:00:00 0 
dtype: int64 
+0

如果你使用这个版本Divakar的回答,不要忘了,你可以标记他的答案接受。干杯。 –

2

一种方法是对基础数组数据使用np.argmax,然后做分级数的最大指数与np.bincount -

np.bincount(df.iloc[:,1:].values.argmax(1), minlength=df.shape[1]-1) 

采样运行 -

In [141]: df 
Out[141]: 
    time-orig 00:15:00 00:30:00 00:45:00 01:00:00 
0 2010-01-04 1164.3 1163.5 1162.8 1161.8 
1 2010-01-05 1186.3 1185.8 1185.6 1185.0 
2 2010-01-06 1181.5 1181.5 1182.7 1182.3 
3 2010-01-07 1202.1 1201.9 1201.7 1200.8 

In [142]: c = np.bincount(df.iloc[:,1:].values.argmax(1), minlength=df.shape[1]-1) 

In [143]: c 
Out[143]: array([3, 0, 1, 0]) 

In [144]: np.c_[df.columns[1:], c] 
Out[144]: 
array([['00:15:00', 3], 
     ['00:30:00', 0], 
     ['00:45:00', 1], 
     ['01:00:00', 0]], dtype=object)