I have a data set containing the pricing data of five brands. I used the following code to generate the number of products across each price segment by defining custom bins and labels.
'''
def distribution_of_sales_price():
sales_price_bins = [0, 50, 100, 150, 200]
sales_price_labels = ['0-50', '50-100', '100-150', '150-200']
df['sales_price_group'] = pd.cut(df['Sales price'], bins=sales_price_bins, labels=sales_price_labels)
sales_price_group = df['sales_price_group'].value_counts().rename_axis('Sales Price Range').reset_index(
name='count')
sales_price_group.to_csv('distribution_of_sales_price.csv', index=False)
return sales_price_group
'''
The output is a csv file with two columns, the first column is the price range, and the second is the count. This code works for all the brands in the data set, now, I need to get the same statistics across each brand in the data set with an additional column named brand. I used the same logic and wrote the code below, but somehow it is not working.
'''
def number_of_products_each_brand_have_across_price_bins():
brands = df['brand'].unique()
main_df = pd.DataFrame(columns=['brand', '0-50', '50-100', '100-150', '150-200'])
for brand in brands:
brand_df = df[df['brand'] == brand]
brand_df['sales_price_group'] = pd.cut(brand_df['Sales price'], bins=[0, 50, 100, 150, 200], labels=['0-50', '50-100', '100-150', '150-200'])
main_df.append({'brand': brand, '0-50': brand_df[brand_df['sales_price_group'] == '0-50']['Sales price'].count(), '50-100': brand_df[brand_df['sales_price_group'] == '50-100']['Sales price'].count(), '100-150': brand_df[brand_df['sales_price_group'] == '100-150']['Sales price'].count(), '150-200': brand_df[brand_df['sales_price_group'] == '150-200']['Sales price'].count()}, ignore_index=True)
main_df.to_csv('number_of_products_each_brand_have_across_price_bins.csv', index=False)
return main_df
'''
The code is supposed to generate a data frame for each brands and then stack it together to form the final output.
You can use the pd.concat to stack the data frames together with axis = 0
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.