Search code examples
jupyter-notebookjupyter-labjupyterhub

Cross-Referencing Tables in Jupyter Notebook


I am having difficulty finding the right command to create a new table that contains the stats of specific players based on the names of players that are in a separate table I made. For example, I have a table that contains the names of multiple NBA players in the table Daddy_Gang and I want to pull their stats from the stats table into a new table that displays the stats of the players from Daddy_Gang.

enter image description here


Solution

  • Quickly, don't post photos of your datasets, code, errors, etc. Post the actual dataset (or a small sample), the actual code, etc. No one really wants to manufacture a dataset and/or your code.

    But, once you get your list you can use .isin().

    Also double check how you are creating that list. You are creating a list of lists. So change that line to:

    g = list(Daddy_Gang.values)

    Code:

    import pandas as pd
    
    stats = pd.DataFrame([
            ['Aaron Gordon', 3, 45],
            ['Coby White', 5, 33],
            ['Zach LaVine', 7, 22],
            ['apad13', 0, 0]],
            columns = ['Player', 'G', 'PTS'])
    
    g = ['Coby White', 'Zach LaVine']
    
    
    g = list(stats['Player'].values)
    
    filtered_df = stats[stats['Player'].isin(g)]
    

    Output:

    print (filtered_df)
            Player  G  PTS
    1   Coby White  5   33
    2  Zach LaVine  7   22