1

I am updating a Pandas Data Frame. The script looks up for a product. If the product is already in data frame, it just updates it columns with accumulated new values. If the product is not there it creates a new set of rows to insert the values of the product.

Code

for m in range(0,len(product_sales_price)):
                if exact_match(str(sales_record[n-1]),str(product_sales_price[m]))==True:

                    total_product_daily_sales = counter * product_sales_price[m+1]
                    '''
                    print(total_product_daily_sales)
                    '''

                    total_product_daily_net_profit = total_product_daily_sales *.1


                    print(counter)
                    print(product_sales_price[m+1])
                    print(total_product_daily_sales)
                    print(total_product_daily_net_profit)
                    print(m)
                    print(product_sales_price[m])


                if (product_revenue_and_net_profit_df.ix[:,0] ==  product_sales_price[m]).any() == True :

                    product_revenue_and_net_profit_df.ix[:,:][(product_revenue_and_net_profit_df.ix[:,
                                                               0] ==  product_sales_price[m])] = [
                        product_revenue_and_net_profit_df.ix[:,0][(product_revenue_and_net_profit_df.ix[:,
                                                               0] ==  product_sales_price[m])],
                        product_revenue_and_net_profit_df.ix[:,1][(product_revenue_and_net_profit_df.ix[:,
                                                               0] ==  product_sales_price[m])]+counter,
                        product_revenue_and_net_profit_df.ix[:,2][(product_revenue_and_net_profit_df.ix[:,
                                                               0] ==  product_sales_price[
                            m])]+total_product_daily_sales,product_revenue_and_net_profit_df.ix[:,
                                                           3][(product_revenue_and_net_profit_df.ix[:,0] ==  product_sales_price[
                            m])]+total_product_daily_net_profit]
                else:

                    product_revenue_and_net_profit_df.ix[(product_revenue_and_net_profit_df.shape[0]+1),:] = (
                        [product_sales_price[m],counter,total_product_daily_sales,
                         total_product_daily_net_profit]
                        )             

Run Time

<sale_frequency time (in seconds):
1
423.44
423.44
42.344
0
Bushwacker Dodge Pocket Style Fender Flare Set of 4

Traceback (most recent call last):
  File "32\scriptStarter.py", line 120, in <module>
  File "C:\Python Projects\Amazon-Sales\amazon_analysis.py", line 162, in <module>
    print (timeit.timeit(fn + "()", "from __main__ import "+fn, number=1))
  File "C:\Users\onthego\Anaconda3\lib\timeit.py", line 219, in timeit
    return Timer(stmt, setup, timer).timeit(number)
  File "C:\Users\onthego\Anaconda3\lib\timeit.py", line 184, in timeit
    timing = self.inner(it, self.timer)
  File "<timeit-src>", line 6, in inner
  File "C:\Python Projects\Amazon-Sales\amazon_analysis.py", line 91, in sale_frequency
m])]+total_product_daily_net_profit]
  File "C:\Users\onthego\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2122, in __setitem__
self._setitem_array(key, value)
  File "C:\Users\onthego\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2142, in _setitem_array
self.ix._setitem_with_indexer(indexer, value)
  File "C:\Users\onthego\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 448, in _setitem_with_indexer
elif np.array(value).ndim == 2:
  File "C:\Users\onthego\Anaconda3\lib\site-packages\pandas\core\series.py", line 521, in __getitem__
result = self.index.get_value(self, key)
  File "C:\Users\onthego\Anaconda3\lib\site-packages\pandas\core\index.py", line 1595, in get_value
    return self._engine.get_value(s, k)
  File "pandas\index.pyx", line 100, in pandas.index.IndexEngine.get_value (pandas\index.c:3113)
  File "pandas\index.pyx", line 108, in pandas.index.IndexEngine.get_value (pandas\index.c:2844)
  File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:3704)
  File "pandas\hashtable.pyx", line 375, in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:7224)
  File "pandas\hashtable.pyx", line 381, in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:7162)
KeyError: 0
>>>
>>>

>>>

SDS
  • 43
  • 1
  • 6
  • What do you think is causing the error? Can you reproduce the error with less code? Can you simplify the variable names in the code for readability? Pasting 20 lines of code out-of-context and a stack trace isn't likely to get a response. – Ben Lerner Sep 16 '15 at 17:24
  • Ben, I am emailing you the code. – SDS Sep 16 '15 at 19:26

0 Answers0