Python中的石斑鱼和轴的长度必须相同 [英] Grouper and axis must be same length in Python

查看:32
本文介绍了Python中的石斑鱼和轴的长度必须相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

  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

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

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?

推荐答案

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

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_bike because it doesn't show the column name.
Instead, you should copy the column into a new dataframe:

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

这篇关于Python中的石斑鱼和轴的长度必须相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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