Converting Object to Dataframe and it is giving me Value, Name, and dtype in each cell
Question:
-
I am running a for loop off a regression per item within a multi-item dataset.
-
I want to output to give me the price point at the max revenue point of the parameters set in the np.arrange
.
-
Here is the code I am working with:
upc_modelfit_summary = []
upcs = df['upc'].unique()
for x in upcs:
df_upc = df[(df['upc'] == x)]
max_val = df_upc['arp_np'].max()
min_val = df_upc['arp_np'].min()
price = np.arange(min_val, max_val, 0.1)
cost = .50
model = ols("units_np ~ arp_np", data=df_upc).fit()
quantity = []
revenue = []
for i in price:
demand = model.params[0] + (model.params[1] * i)
quantity.append(demand)
revenue.append((i-cost) * demand)
profit = pd.DataFrame({"Price": price, "Revenue": revenue, "Quantity": quantity, "UPC": x})
max_val = profit.loc[(profit['Revenue'] == profit['Revenue'].max())]
upc_modelfit_summary.append(
{
'Price': max_val['Price'],
'Revenue': max_val['Revenue'],
'Quantity': max_val['Quantity'],
'UPC': max_val['UPC']
})
a= pd.DataFrame(upc_modelfit_summary)
a
I would like to create a dataframe that compiles the price, revenue, quantity, and upc for each item that is looped.
For some reason when converting this to a dataframe it is giving me the values, names, and dtypes of each cell value (see image)
Is there a better way to do this? Ideally the end result would be a df that could be exported in some fashion.
Price Revenue Quantity UPC
0 4 4.49 Name: Price, dtype: float64 4 3.99 Name: Revenue, dtype: float64 4 1.0 Name: Quantity, dtype: float64 4 00-28400-04330 Name: UPC, dtype: object
Answers:
The issue is that you’re adding the entire data-frame max_val[Col]
into the upc_modelfit_summary
dictionary instead of just the value. Change the upc_modelfit_summary.append()
line to this:
upc_modelfit_summary.append(
{
'Price': max_val['Price'].values[0] if max_val['Price'].values.size > 0 else np.nan,
'Revenue': max_val['Revenue'].values[0] if max_val['Revenue'].values.size > 0 else np.nan,
'Quantity': max_val['Quantity'].values[0] if max_val['Quantity'].values.size > 0 else np.nan,
'UPC': max_val['UPC'].values[0] if max_val['UPC'].values.size > 0 else np.nan
})
-
I am running a for loop off a regression per item within a multi-item dataset.
-
I want to output to give me the price point at the max revenue point of the parameters set in the
np.arrange
. -
Here is the code I am working with:
upc_modelfit_summary = []
upcs = df['upc'].unique()
for x in upcs:
df_upc = df[(df['upc'] == x)]
max_val = df_upc['arp_np'].max()
min_val = df_upc['arp_np'].min()
price = np.arange(min_val, max_val, 0.1)
cost = .50
model = ols("units_np ~ arp_np", data=df_upc).fit()
quantity = []
revenue = []
for i in price:
demand = model.params[0] + (model.params[1] * i)
quantity.append(demand)
revenue.append((i-cost) * demand)
profit = pd.DataFrame({"Price": price, "Revenue": revenue, "Quantity": quantity, "UPC": x})
max_val = profit.loc[(profit['Revenue'] == profit['Revenue'].max())]
upc_modelfit_summary.append(
{
'Price': max_val['Price'],
'Revenue': max_val['Revenue'],
'Quantity': max_val['Quantity'],
'UPC': max_val['UPC']
})
a= pd.DataFrame(upc_modelfit_summary)
a
I would like to create a dataframe that compiles the price, revenue, quantity, and upc for each item that is looped.
For some reason when converting this to a dataframe it is giving me the values, names, and dtypes of each cell value (see image)
Is there a better way to do this? Ideally the end result would be a df that could be exported in some fashion.
Price Revenue Quantity UPC
0 4 4.49 Name: Price, dtype: float64 4 3.99 Name: Revenue, dtype: float64 4 1.0 Name: Quantity, dtype: float64 4 00-28400-04330 Name: UPC, dtype: object
The issue is that you’re adding the entire data-frame max_val[Col]
into the upc_modelfit_summary
dictionary instead of just the value. Change the upc_modelfit_summary.append()
line to this:
upc_modelfit_summary.append(
{
'Price': max_val['Price'].values[0] if max_val['Price'].values.size > 0 else np.nan,
'Revenue': max_val['Revenue'].values[0] if max_val['Revenue'].values.size > 0 else np.nan,
'Quantity': max_val['Quantity'].values[0] if max_val['Quantity'].values.size > 0 else np.nan,
'UPC': max_val['UPC'].values[0] if max_val['UPC'].values.size > 0 else np.nan
})