Intern
**Python tasks** A Pandas DataFrame with data from orders_log is called df. 1) Filter the rows: status "processing" AND revenue > 50000 AND priority "high". Display the result. 2) Create a column 'revenue_group' according to the rules: ‘Small’ (< 50000), ‘Medium’ (50000 <= revenue < 90000), ‘Large’ (>= 90000). Then count the number of orders in each group by categories (Category) and display the result. 1) df[(df["state"] == "processing") & (df["revenue"] > 50000) & (df["priority"] == "high")] 2)
sobes.tech AI
Answer from AI
- To create the
revenue_groupcolumn, you can use thepd.cutornp.selectfunction. Then, group the data by categories and count the number of orders:
import numpy as np
conditions = [
df['revenue'] < 50000,
(df['revenue'] >= 50000) & (df['revenue'] < 90000),
df['revenue'] >= 90000
]
choices = ['Small', 'Medium', 'Large']
df['revenue_group'] = np.select(conditions, choices)
# Count the number of orders in each group by category
result = df.groupby(['Category', 'revenue_group']).size().reset_index(name='order_count')
print(result)
This code will create a new column with revenue groups and output a table with the number of orders for each category and revenue group.