Код: Выделить всё
Filter rows where Category is either "A" or "B".
Further filter these rows to include only those where Value is greater than 10.
Group the filtered data by SubCategory and calculate the sum of Value for each SubCategory.
Sort the results by the summed Value in descending order.
Код: Выделить всё
data = {
'Category': ['A', 'B', 'A', 'C', 'B', 'A'],
'SubCategory': ['X', 'Y', 'X', 'Z', 'X', 'Y'],
'Value': [5, 15, 20, 25, 10, 30],
'Date': pd.to_datetime(['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05', '2023-01-06'])
}
df = pd.DataFrame(data)
Код: Выделить всё
filtered_df = df[(df['Category'].isin(['A', 'B'])) & (df['Value'] > 10)]
grouped_df = filtered_df.groupby('SubCategory')['Value'].sum().reset_index()
sorted_df = grouped_df.sort_values(by='Value', ascending=False)
print(sorted_df)
Подробнее здесь: https://stackoverflow.com/questions/787 ... with-multi