简体   繁体   中英

“ValueError: Length of values does not match length of index”

I am trying to Group by customers and price bin columns to calculate Sales/Orders the code is

# 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)

the error produced is

---------------------------------------------------------------------------
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)

Here is an attachment of my google colab file so that you can see what i am doing trying to do

I am not able to run the code, but here's something you can try.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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