pandas Python中的石斑鱼和轴的长度必须相同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50884704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:42:22  来源:igfitidea点击:

Grouper and axis must be same length in Python

pythonpandaspandas-groupby

提问by Rowling

I am a beginner of Python, and I study a textbook to learn the Pandas module. I have a dataframe called Berri_bike, and it is from the following code:

我是 Python 的初学者,我正在学习一本教科书来学习 Pandas 模块。我有一个名为 Berri_bike 的数据框,它来自以下代码:

  bike_df=pd.read_csv(os.path.join(path,'comptagevelo2012.csv'),parse_dates=['Date'],\
                          encoding='latin1',dayfirst=True,index_col='Date')



  Berri_bike=bike_df['Berri1'].copy() # get only the column='Berri1'

  Berri_bike['Weekday']=Berri_bike.index.weekday

  weekday_counts = Berri_bike.groupby('Weekday').aggregate(sum)
  weekday_counts

I have 3 columns in my Berri_bilk , a data index- from 1/1/2012 to 12/31/2012, and value column with numbers for each data, and a weekday column I assigned to it. But when I want to group by the values, I got the error: ValueError: Grouper and axis must be same length, I am not sure what this mean, what I want to do is very simple, like in SQL, sum(value) grouped weekday... can anyone please let me know what happended here?

我的 Berri_bilk 中有 3 列,一个数据索引 - 从 1/1/2012 到 12/31/2012,以及每个数据带有数字的值列,以及我分配给它的工作日列。但是当我想按值分组时,出现错误:ValueError: Grouper and axis must be same length,我不确定这是什么意思,我想做的很简单,就像在 SQL 中一样,sum(value)工作日分组...谁能告诉我这里发生了什么?

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by Mr. T

You copy your column into a pandas series instead of a new dataframe, hence the following operations behave differently. You can see this if you print out Berri_bikebecause it doesn't show the column name.
Instead, you should copy the column into a new dataframe:

您将列复制到 Pandas 系列而不是新数据帧中,因此以下操作的行为有所不同。如果您打印出来,您可以看到这一点,Berri_bike因为它没有显示列名。
相反,您应该将该列复制到一个新的数据框中

import pandas as pd
df = pd.DataFrame(np.random.randint(0, 30, size = (70, 2)),
                  columns = ["A", "B"],
                  index = pd.date_range("20180101", periods = 70))

Berri_bike = df[["A"]]

Berri_bike['Weekday'] = Berri_bike.index.weekday

weekday_counts = Berri_bike.groupby("Weekday").sum()
print(weekday_counts)
#sample output
          A
Weekday    
0        148
1        101
2        127
3        139
4        163
5         74
6        135