1

我正在更新 Pandas 数据框。该脚本查找产品。如果产品已经在数据框中,它只是用累积的新值更新它的列。如果产品不存在,它会创建一组新的行来插入产品的值。

代码

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

运行

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

>>>

4

0 回答 0