使用熊猫的最小和最大频率?

时间:2020-01-21 07:05:47

标签: python pandas

是否可以使用熊猫找到最小和最大频率?我有一系列值,我想知道它出现的最小和最大频率。以1为例,在24个计数中出现3次。因此,平均频率为3/24或1/8。可以从1 /总数中得出。

但是,我正在寻找的是找到1的最小值和最大值:

  • min:0(其他值在第一个1和第二个1之间出现的次数)
  • max:14(其他值在第二个1和第三个1之间出现的次数)

样本DF:

╔════╗
║ X  ║
╠════╣
║  1 ║
║  1 ║
║  8 ║
║  5 ║
║  8 ║
║ 11 ║
║  7 ║
║ 11 ║
║ 12 ║
║  7 ║
║  2 ║
║  2 ║
║  6 ║
║  7 ║
║  9 ║
║  2 ║
║  1 ║
║  3 ║
║ 10 ║
║  2 ║
║ 10 ║
║ 13 ║
║  4 ║
║  6 ║
╚════╝
data = {'X':[1,1,8,5,8,11,7,11,12,7,2,2,6,7,9,2,1,3,10,2,10,13,4,6]}

非常感谢

2 个答案:

答案 0 :(得分:2)

使用:

#changed sample data for possible non 1 before first 1 occurence
df = pd.DataFrame(data = {'X':[5,8,1,1,8,5,8,11,7,11,12,7,2,2,6,7,9,2,1,3,10,2,10,13,4,6]})
#print (df)

您可以按Series.eq比较值,并按Series.cumsum创建组,用0删除组(如果在第一个1之前存在某些值)和最后一个组(如果最后一个也必须删除)列的值是1),Series.isin~,掩码是1,然后使用Series.value_counts减去s = df['X'].eq(1).cumsum() s = s[~s.isin([0, s.max()])].value_counts().sub(1) print (s) 2 14 1 0 Name: X, dtype: int64

min1 = s.min()
max1 = s.max()
print (min1)
0
print (max1)
14

最后获得最小值和最大值:

1

编辑:

如果还需要先进行s = df['X'].eq(1).cumsum().value_counts().sort_index().iloc[:-1].sub(1) print (s) min1 = s.min() max1 = s.max() print (min1) print (max1) 分组,然后使用:

Show(Window owner, string messageBoxText);
Show(Window owner, string messageBoxText, string caption);
Show(Window owner, string messageBoxText, string caption, MessageBoxButton button);
Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon);
Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult);
Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options);

答案 1 :(得分:0)

这是@jezrael的改进答案,它将遍历所有值并打印出最小和最大。

df = pd.DataFrame(data = {'X':[1,5,8,1,8,5,8,11,7,11,12,7,2,2,6,7,9,2,1,3,10,2,10,13,4,6]})
#print (df)
for i in set(df['X']):
    s = df['X'].eq(i).cumsum()
    s = s[~s.isin([0, s.max()])].value_counts().sub(1)
    min1 = s.min()
    max1 = s.max()
    if math.isnan(min1):
        min1=max1=0
    print(f"min for {i} is {min1}")
    print(f"max for {i} is {max1}")
相关问题