首页 > 解决方案 > “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文件的附件,以便您可以看到我正在尝试做的事情

标签: pythonpandascluster-analysis

解决方案


我无法运行代码,但您可以尝试以下方法。

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']

推荐阅读