[Python] Simplify Code for Grouping, Filtering, and Pie Chart of Game Publisher Sales

Discussão em 'Python' iniciado por Stack, Julho 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm currently learning how to use pandas, and I feel like my code could be simplified or made more efficient. I use this data:https://www.kaggle.com/datasets/gregorut/videogamesales

    Pb_Sales = df.groupby('Publisher')[['Global_Sales']].sum().sort_values('Global_Sales',ascending=False)
    total = Pb_Sales['Global_Sales'].sum()
    Pb_Sales['Proportion']=Pb_Sales['Global_Sales']/total
    index_position = 0
    threshold = 0.02
    index_list = list(Pb_Sales.index)
    for num in Pb_Sales['Proportion']:
    if num < threshold:
    index_list[index_position]= 'Other'
    index_position=index_position+1
    Pb_Sales.index = index_list

    Pb_Sales = Pb_Sales.groupby(Pb_Sales.index).sum().sort_values('Global_Sales',ascending = False)
    plt.title("most profitable publisehr")
    plt.ylabel("")
    chart_Pie= Pb_Sales['Global_Sales'].plot(kind = 'pie',figsize=(10,5),legend = False)
    chart_Pie.set_ylabel("")
    plt.show()


    Also,I followed a tutorial to write this code below , but I later read online that it's not recommended to use it — though I'm not sure why.

    df['Year'] = df['Year'].astype(int) #I want to make the Year column value to be int not float

    Continue reading...

Compartilhe esta Página