python - “ValueError:值的长度与索引的长度不匹配”
问题描述
我正在尝试按客户和价格箱列分组以计算销售/订单代码是
# Group by customers and price bins and calculate Sales/Order
df_by_customer_price = df_analysis.groupby(['CustomerID','price_equal_bin'],\
as_index=False).agg({'Sales':'sum','InvoiceNo':'nunique'})
df_by_customer_price['Sales/Order'] = df_by_customer_price['Sales']/\
df_by_customer_price['InvoiceNo']
df_by_customer_price.head(3)
产生的错误是
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-148-29dbd9251ee7> in <module>()
1 # Group by customers and price bins and calculate Sales/Order
----> 2 df_by_customer_price = df_analysis.groupby(['CustomerID','price_equal_bin'], as_index=False).agg({'Sales':'sum','InvoiceNo':'nunique'})
3 df_by_customer_price['Sales/Order'] = df_by_customer_price['Sales']/ df_by_customer_price['InvoiceNo']
4
5 df_by_customer_price.head(3)
4 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/internals/construction.py in sanitize_index(data, index)
746 if len(data) != len(index):
747 raise ValueError(
--> 748 "Length of values "
749 f"({len(data)}) "
750 "does not match length of index "
ValueError: Length of values (33647) does not match length of index (43240)
这是我的google colab文件的附件,以便您可以看到我正在尝试做的事情
解决方案
我无法运行代码,但您可以尝试以下方法。
df_by_customer_price = df_analysis.groupby(['CustomerID','price_equal_bin'],\
as_index=False).agg({'Sales':'sum','InvoiceNo':'nunique'})
df_by_customer_price.reset_index(inplace = True)
df_by_customer_price['Sales/Order'] = df_by_customer_price['Sales']/
df_by_customer_price['InvoiceNo']
推荐阅读
- javascript - CSS 变换或缩放?安装动画
- webpack - 使用 quasar 引用 app.sass 中的静态文件
- c# - 从字符串压缩和压缩文件
- asp.net - 为什么操作链接没有响应?
- python - 根据第三个变量中的多个条件,在数据框中为多个子组创建一个新变量
- django - collectstatic 命令在部署到弹性 beantalk 时失败 | 使用 django-storages 在 S3 上存储静态文件
- sql-server - SQL Server:仅显示满足条件的值
- windows-10 - Google Cloud SDK 安装错误:此时文件是意外的
- java - Azure sql 连接使用 JTDS 挂起
- python - 在 Azure Batch 中运行任务时保存输出(使用 python SDK)