在大 pandas DataFrame中查找并​​选择最常用的列数据 [英] find and select the most frequent data of column in pandas DataFrame

查看:242
本文介绍了在大 pandas DataFrame中查找并​​选择最常用的列数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下列的数据框:

  file ['DirViento'] 

Fecha
2011-01-01 ENE
2011-01-02 ENE
2011-01-03 ENE
2011-01-04 NNE
2011-01-05 ENE
2011-01-06 ENE
2011-01-07 ENE
2011-01-08 ENE
2011-01-09 NNE
2011-01-10 ENE
2011-01-11 ENE
2011-01-12 ENE
2011-01-13 ESE
2011-01-14 ENE
2011-01-15 ENE
...
2011-12-17 ENE
2011-12-18 ENE
2011-12-19 ENE
2011-12-20 ENE
2011-12-21 ENE
2011-12-22 ENE
2011-12-23 ENE
2011-12-24 ENE
2011-12-25 ENE
2011-12-26 ESE
2011-12-27 ENE
2011-12-28 NE
2011-12-29 ENE
2011-12-30 NNE
2011-12-31 ENE
名称:DirViento,长度:290,dtype:对象

该列每年每月记录风向。我试图获得每个月的主导方向。要完成此操作,请选择一个月内最常重复的数据:

  file ['DirViento']。groupby(lambda x: x.month).value_counts()


1 ENE 23
NNE 6
E 1
ESE 1
2 ENE 21
NNO 3
NNE 2
NE 1
3 ENE 21
OSO 1
ESE 1
SSE 1
4 ENE 21
NNE 2
ESE 1
NNO 1
6 ENE 15
ESE 2
SSE 2
ONO 1
E 1
7 ENE 22
ONO 1
OSO 1
NE 1
NNE 1
NNO 1
8 ENE 23
NNE 5
NE 1
ONO 1
ESE 1
9 ENE 17
NNE 7
ONO 2
NE 1
E 1
ESE 1
NNO 1
10 ENE 16
NNE 2
ESE 2
NNO 2
ONO 1
NE 1
E 1
11 ENE 13
NNE 2
ESE 2
ONO 1
12 ENE 26
NNE 3
NE 1
ESE 1
长度:54,dtype:int64

运行以下代码行

  wind_moda = file ['DirViento']。groupby(lambda x:x .month).agg(lambda x:stats.mode(x)[0] [0])

应该得到这样的东西

  1 ENE 
2 ENE
3 ENE
4 ENE
6 ENE
7 ENE
8 ENE
9 ENE
10 ENE
11 ENE
12 ENE

但是我得到以下内容:

  1 E 
2 ENE
3 ENE
4 ENE
6 E
7 ENE
8 ENE
9 E
10 E
11 ENE
12 ENE

为什么在12个月的4个月内没有考虑到最常见的数据?



我做错了什么?



任何想法,以获得每月最常见的数据?as>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法。这可能是有用的,因为我正在寻找这个。



这里是文档



编辑:对于价值:

  DataFrame.mode()[0] 


I have a dataframe with the following column:

file['DirViento']

Fecha
2011-01-01    ENE
2011-01-02    ENE
2011-01-03    ENE
2011-01-04    NNE 
2011-01-05    ENE
2011-01-06    ENE
2011-01-07    ENE
2011-01-08    ENE
2011-01-09    NNE
2011-01-10    ENE
2011-01-11    ENE
2011-01-12    ENE
2011-01-13    ESE
2011-01-14    ENE
2011-01-15    ENE
... 
2011-12-17    ENE
2011-12-18    ENE
2011-12-19    ENE
2011-12-20    ENE
2011-12-21    ENE
2011-12-22    ENE
2011-12-23    ENE
2011-12-24    ENE
2011-12-25    ENE
2011-12-26    ESE
2011-12-27    ENE
2011-12-28     NE
2011-12-29    ENE
2011-12-30    NNE
2011-12-31    ENE
Name: DirViento, Length: 290, dtype: object

The column has daily records of wind direction for each month of the year. I'm trying to get the dominant direction for each month. To accomplish this, select the data most often repeated during the month:

file['DirViento'].groupby(lambda x: x.month).value_counts()


1   ENE    23
    NNE     6
    E       1
    ESE     1
2   ENE    21
    NNO     3
    NNE     2
    NE      1
3   ENE    21
    OSO     1
    ESE     1
    SSE     1
4   ENE    21
    NNE     2
    ESE     1
    NNO     1
6   ENE    15
    ESE     2
    SSE     2
    ONO     1
    E       1
7   ENE    22
    ONO     1
    OSO     1
    NE      1
    NNE     1
    NNO     1
8   ENE    23
    NNE     5
    NE      1
    ONO     1
    ESE     1
9   ENE    17
    NNE     7
    ONO     2
    NE      1
    E       1
    ESE     1
    NNO     1
10  ENE    16
    NNE     2
    ESE     2
    NNO     2
    ONO     1
    NE      1
    E       1
11  ENE    13
    NNE     2
    ESE     2
    ONO     1
12  ENE    26
    NNE     3
    NE      1
    ESE     1
Length: 54, dtype: int64

When running the following line of code

wind_moda=file['DirViento'].groupby(lambda x: x.month).agg(lambda x: stats.mode(x)[0][0])

Should get something like this

     1  ENE    
     2  ENE    
     3  ENE  
     4  ENE
     6  ENE
     7  ENE    
     8  ENE    
     9  ENE
    10  ENE  
    11  ENE
    12  ENE  

But I get the following:

 1          E  
 2        ENE  
 3        ENE  
 4        ENE  
 6          E  
 7        ENE  
 8        ENE  
 9          E  
 10         E  
 11       ENE  
 12       ENE  

Why in 4 of the 12 months is not taking into account the most frequent data?

Am I doing something wrong ?

Any idea to get the most common data each month?

解决方案

Pandas 0.15.2 has a DataFrame.mode() method. It might be of use to someone looking for this as I was.

Here are the docs.

Edit: For the Value:

DataFrame.mode()[0]

这篇关于在大 pandas DataFrame中查找并​​选择最常用的列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆