Загрузка данных


# 3.1
df_multi = df.set_index(['category', 'product'])
print(df_multi.head())

# 3.2
print(df_multi.loc['Electronics'].head())
print(df_multi.loc[('Electronics', 'A')].head())
print(df_multi.xs('B', level='product').head())

# 3.3
df_swapped = df_multi.swaplevel()
print(df_swapped.head())

df_sorted = df_multi.sort_index(level='product')



print(df_sorted.head())

print(df['category'].dtype)

df['category'] = df['category'].astype('category')
print(df['category'].dtype)

ratings = pd.Categorical(df['rating'].round(),
                         categories=[1,2,3,4,5],
                         ordered=True)

df['rating_cat'] = ratings

# переименование
df['category'] = df['category'].cat.rename_categories({
    'Electronics': 'Электроника',
    'Clothing': 'Одежда',
    'Food': 'Еда',
    'Books': 'Книги'
})

# добавление и удаление
df['category'] = df['category'].cat.add_categories(['Toys'])
df['category'] = df['category'].cat.remove_categories(['Toys'])

print(df['category'].value_counts())
print(df.groupby('category')['sales'].mean())