Facing an Error in Python Pandas - must have equal len keys and value when setting with an iterable
I am not an expert in Python and pandas, I have mentioned my codebase below, my code works completely fine for a single row but there is a small issue I am facing when applying it to every row in DataFrame, it is not returning individual values, I get value error if I tried to assign it to multiple columns.
Error i am facing:
Traceback (most recent call last):
File "test.py", line 12, in
pricerule_calculate_run = PriceruleCalculate.run(method='business')
File ".....", line 940, in run
__setupProducts(productUUID)
File ".....", line 507, in __setupProducts
]] = panda_dataframe[['uuid']].apply(lambda x:__getDistributorStockData(x), axis=1)
File "/usr/local/lib/python3.6/site-packages/pandas/core/frame.py", line 2514, in __setitem__
self._setitem_array(key, value)
File "/usr/local/lib/python3.6/site-packages/pandas/core/frame.py", line 2544, in _setitem_array
self.loc._setitem_with_indexer((slice(None), indexer), value)
File "/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py", line 599, in _setitem_with_indexer
raise ValueError('Must have equal len keys and value '
ValueError: Must have equal len keys and value when setting with an iterable
Code:
panda_dataframe[[
'distributor-stock-cost-price-real-distributor',
'distributor-stock-cost-price-stock',
'distributor-stock-cost-price-expected',
'distributor-stock-cost-price-country',
'distributor-stock-cost-price-real-distributor',
'distributor-stock-cost-price-real-stock',
'distributor-stock-cost-price-real-expected',
'distributor-stock-cost-price-real-country'
]] = panda_dataframe[['uuid']].apply(lambda x:__getDistributorStockData(x))
And then i got my function i apply to my dataframe.
def __getDistributorStockData(x):
in_product_uuid = x
out_cost_price_dist = None
out_cost_price_stock = None
out_cost_price_expected = None
out_cost_price_country = None
out_cost_price_real_dist = None
out_cost_price_real_stock = None
out_cost_price_real_expected = None
out_cost_price_real_country = None
try:
product_data = store_remote_stock_dataframe.get_group(in_product_uuid)
product_data_onstock = product_data.loc[product_data['Stock'] > 0, ['Stock', 'CostPriceReal', 'CostPrice', 'Expected', 'DistributorUUID', 'Country']]
product_data_outstock = product_data.loc[product_data['Stock'] <= 0, ['Stock', 'CostPriceReal', 'CostPrice', 'Expected', 'DistributorUUID', 'Country']]
if len(product_data_onstock) > 0:
stock_cost_price = product_data_onstock.sort_values(by=['CostPrice'], ascending=True).iloc[0,:]
stock_cost_real_price = product_data_onstock.sort_values(by=['CostPriceReal'], ascending=True).iloc[0,:]
out_cost_price_dist = stock_cost_price['DistributorUUID']
out_cost_price_stock = stock_cost_price['Stock']
out_cost_price_expected = stock_cost_price['Expected']
out_cost_price_country = stock_cost_price['Country']
out_cost_price_real_dist = stock_cost_real_price['DistributorUUID']
out_cost_price_real_stock = stock_cost_real_price['Stock']
out_cost_price_real_expected = stock_cost_real_price['Expected']
out_cost_price_real_country = stock_cost_real_price['Country']
elif len(product_data_outstock) > 0:
stock_cost_price = product_data_outstock.sort_values(by=['CostPrice'], ascending=True).iloc[0,:]
stock_cost_real_price = product_data_outstock.sort_values(by=['CostPriceReal'], ascending=True).iloc[0,:]
out_cost_price_dist = stock_cost_price['DistributorUUID']
out_cost_price_stock = stock_cost_price['Stock']
out_cost_price_expected = stock_cost_price['Expected']
out_cost_price_country = stock_cost_price['Country']
out_cost_price_real_dist = stock_cost_real_price['DistributorUUID']
out_cost_price_real_stock = stock_cost_real_price['Stock']
out_cost_price_real_expected = stock_cost_real_price['Expected']
out_cost_price_real_country = stock_cost_real_price['Country']
else:
stock_cost_price = None
stock_cost_real_price = None
except:
stock_cost_price = None
stock_cost_real_price = None
return [
out_cost_price_dist,
out_cost_price_stock,
out_cost_price_expected,
out_cost_price_country,
out_cost_price_real_dist,
out_cost_price_real_stock,
out_cost_price_real_expected,
out_cost_price_real_country ]
Error i am facing:
Traceback (most recent call last):
File "test.py", line 12, in
pricerule_calculate_run = PriceruleCalculate.run(method='business')
File ".....", line 940, in run
__setupProducts(productUUID)
File ".....", line 507, in __setupProducts
]] = panda_dataframe[['uuid']].apply(lambda x:__getDistributorStockData(x), axis=1)
File "/usr/local/lib/python3.6/site-packages/pandas/core/frame.py", line 2514, in __setitem__
self._setitem_array(key, value)
File "/usr/local/lib/python3.6/site-packages/pandas/core/frame.py", line 2544, in _setitem_array
self.loc._setitem_with_indexer((slice(None), indexer), value)
File "/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py", line 599, in _setitem_with_indexer
raise ValueError('Must have equal len keys and value '
ValueError: Must have equal len keys and value when setting with an iterable
Code:
panda_dataframe[[
'distributor-stock-cost-price-real-distributor',
'distributor-stock-cost-price-stock',
'distributor-stock-cost-price-expected',
'distributor-stock-cost-price-country',
'distributor-stock-cost-price-real-distributor',
'distributor-stock-cost-price-real-stock',
'distributor-stock-cost-price-real-expected',
'distributor-stock-cost-price-real-country'
]] = panda_dataframe[['uuid']].apply(lambda x:__getDistributorStockData(x))
And then i got my function i apply to my dataframe.
def __getDistributorStockData(x):
in_product_uuid = x
out_cost_price_dist = None
out_cost_price_stock = None
out_cost_price_expected = None
out_cost_price_country = None
out_cost_price_real_dist = None
out_cost_price_real_stock = None
out_cost_price_real_expected = None
out_cost_price_real_country = None
try:
product_data = store_remote_stock_dataframe.get_group(in_product_uuid)
product_data_onstock = product_data.loc[product_data['Stock'] > 0, ['Stock', 'CostPriceReal', 'CostPrice', 'Expected', 'DistributorUUID', 'Country']]
product_data_outstock = product_data.loc[product_data['Stock'] <= 0, ['Stock', 'CostPriceReal', 'CostPrice', 'Expected', 'DistributorUUID', 'Country']]
if len(product_data_onstock) > 0:
stock_cost_price = product_data_onstock.sort_values(by=['CostPrice'], ascending=True).iloc[0,:]
stock_cost_real_price = product_data_onstock.sort_values(by=['CostPriceReal'], ascending=True).iloc[0,:]
out_cost_price_dist = stock_cost_price['DistributorUUID']
out_cost_price_stock = stock_cost_price['Stock']
out_cost_price_expected = stock_cost_price['Expected']
out_cost_price_country = stock_cost_price['Country']
out_cost_price_real_dist = stock_cost_real_price['DistributorUUID']
out_cost_price_real_stock = stock_cost_real_price['Stock']
out_cost_price_real_expected = stock_cost_real_price['Expected']
out_cost_price_real_country = stock_cost_real_price['Country']
elif len(product_data_outstock) > 0:
stock_cost_price = product_data_outstock.sort_values(by=['CostPrice'], ascending=True).iloc[0,:]
stock_cost_real_price = product_data_outstock.sort_values(by=['CostPriceReal'], ascending=True).iloc[0,:]
out_cost_price_dist = stock_cost_price['DistributorUUID']
out_cost_price_stock = stock_cost_price['Stock']
out_cost_price_expected = stock_cost_price['Expected']
out_cost_price_country = stock_cost_price['Country']
out_cost_price_real_dist = stock_cost_real_price['DistributorUUID']
out_cost_price_real_stock = stock_cost_real_price['Stock']
out_cost_price_real_expected = stock_cost_real_price['Expected']
out_cost_price_real_country = stock_cost_real_price['Country']
else:
stock_cost_price = None
stock_cost_real_price = None
except:
stock_cost_price = None
stock_cost_real_price = None
return [
out_cost_price_dist,
out_cost_price_stock,
out_cost_price_expected,
out_cost_price_country,
out_cost_price_real_dist,
out_cost_price_real_stock,
out_cost_price_real_expected,
out_cost_price_real_country ]