text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png
import matplotlib.pyplot as plt

import pandas as pd

import seaborn as sns

import pandas as pd
import numpy as np
df = pd.read_csv('animes.csv')

for c in df.columns[12:]:
    df[c] = df[c].astype('int')
idx = []
for i in df.columns:
    idx.append(i.replace('genre_', ''))
df.columns = idx

import seaborn as sns
import matplotlib.pyplot as plt

number_genre = df[df.columns[12:]].sum(axis=1).value_counts().sort_index()
colors = []
maxg = max(number_genre)
for n in number_genre:
    x = 0.8 - n / (2 * maxg)
    colors.append((0.7, x, x))
average = number_genre.mean()
plt.text(9.6, 120, 'average', fontsize=14)

def transf(x):
    if x == 0:
        return 1
    else:
        return x
def weight_df(df, col_start=12):
    fact = df[df.columns[col_start:]].sum(axis=1).apply(lambda x: transf(x))
    df_va = df.values
    for m in range(len(df_va)):
        df_va[m]
        for i in range(col_start, len(df_va[m])):
            df_va[m][i] = df_va[m][i] / fact[m]
    return pd.DataFrame(df_va, columns=df.columns)
lst = [['anime 1', 1, 1, 0, 1, 1, 0], ['anime 2', 0, 0, 0, 0, 0, 1], ['anime 3', 1, 0, 1, 1, 0, 0]]
cols = ['Anime', 'category_1', 'category_2', 'category_3', 'category_4', 'category_5', 'category_6']
example = pd.DataFrame(lst, columns=cols)
example

df_weighted = weight_df(df)
nb_0_genre = (df[df.columns[12:]].sum(axis=1) == 0).sum()
weighted_betw = df_weighted[df_weighted.columns[12:]].sum()
weighted_betw['NO genre'] = nb_0_genre
distrib_genre = 100 * weighted_betw / weighted_betw.sum()
distrib_genre = distrib_genre.sort_values(ascending=False)

def create_bins(v):
    if v > 10000:
        return '>10000'
    elif v > 2000:
        return '2000-10000'
    elif v > 500:
        return '500-2000'
    elif v > 100:
        return '100-500'
    elif v >= 10:
        return '10-100'
    else:
        return '<10'
df['votes_cat'] = df['votes'].apply(create_bins)