简体   繁体   English

无法在 dataframe 的 pandas 中使用 groupby function

[英]unable to use groupby function in pandas for dataframe

I have a dataframe with 100+ rows, where i need to extract values w.r.t 'High' column using groupby.我有一个 dataframe 超过 100 行,我需要使用 groupby 提取值 w.r.t 'High' 列。 But I am unable to do it.但我无法做到。

My dataframe sample is:我的 dataframe 样品是:

           Date     Open     High  ...    Volume              
           Date     Open     High  ...    Volume 
0    24-04-2020  1840.10  1851.60  ...     93384      
1    23-04-2020  1786.00  1891.70  ...    453645     
2    22-04-2020  1746.00  1789.10  ...    103696      
3    21-04-2020  1775.00  1794.00  ...    149222

I have stored the dataframe in a variable data_frame and when I try using data_frame.groupby('High') , I am getting the following error message:我已将 dataframe 存储在变量data_frame中,当我尝试使用data_frame.groupby('High')时,我收到以下错误消息:

"ValueError: Grouper for 'High' not 1-dimensional". “ValueError:‘高’而不是一维的分组器”。

Any idea how I can fix this?知道如何解决这个问题吗?

I think the problem is with the header, the header has multi-index and so specifying only one column name gives you error.我认为问题在于 header,header 具有多索引,因此仅指定一个列名会给您带来错误。 You can set the column headers using the df.columns .您可以使用df.columns设置列标题。 If you want to access the High column, then simple use loc or `data_frame['High']如果要访问High列,则简单地使用loc或 `data_frame['High']

data_frame.columns = ['Date', 'Open', 'High', 'Volume'] #add more 

If you want to get the max value of the High column based on the date, then you have to use groupby on Date and project the High column.如果要根据日期获取High列的最大值,则必须在Date上使用 groupby 并投影High列。

data_frame.groupby(['Date'])['High'].max()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM