[Python] How do you remove already filtered category values from DataFrames from plots and...

Discussão em 'Python' iniciado por Stack, Agosto 24, 2025.

  1. Stack

    Stack Membro Participativo

    My dataframes show video game titles, platforms, year of release, revenue, etc.

    I have filtered the original dataframe "df_samplegames", which has 29 different platforms (type category), into a new dataframe "df_finalgames" that should only include the 6 platforms: '3DS', 'PSV', 'WiiU', 'PS4', 'XOne', 'PC'.

    # Original dataframe:
    df_samplegames['platform'].unique()

    # Result:
    ['GEN', 'NES', 'NG', 'SCD', 'SNES', ..., '3DS', 'PSV', 'WiiU', 'PS4', 'XOne']
    Length: 29
    Categories (29, object): ['GEN', 'NES', 'NG', 'SCD', ..., 'PSV', 'WiiU', 'PS4', 'XOne']


    # New dataframe:
    df_finalgames = df_samplegames[df_samplegames['platform'].isin(
    ['3DS', 'PSV', 'WiiU', 'PS4', 'XOne', 'PC'])].sort_values(by = ['year', 'platform']).reset_index(drop = True)
    df_finalgames['platform'].unique()

    # Result:
    ['PC', '3DS', 'PSV', 'WiiU', 'PS4', 'XOne']
    Categories (6, object): ['PC', '3DS', 'PSV', 'WiiU', 'PS4', 'XOne']


    But whenever I want to use the latter dataframe to create a pairplot, or a pivot table, it keeps including the filtered out platform categories.

    # Pivot table creation:
    df_platgames = df_finalgames[['name', 'platform', 'total_sales']]
    pvt_platgames = df_platgames.pivot_table(
    values = 'total_sales', index = 'name', columns = 'platform', aggfunc = 'sum')
    pvt_platgames

    # Result, note the column headers:
    platform 2600 3DO 3DS DC DS GB GBA GC GEN GG ... SAT SCD SNES TG16 WS Wii WiiU X360 XB XOne
    name
    Beyblade Burst 0.0 0.0 0.03 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
    Fire Emblem Fates 0.0 0.0 1.67 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
    Frozen: Olaf's Quest 0.0 0.0 0.59 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
    # Etc...


    How can I force my code to ignore those platforms that I don't need? Thank you all in advance.

    Continue reading...

Compartilhe esta Página