[Python] pandas DataFrame and pandas.groupby to calculate Salaries

Discussão em 'Python' iniciado por Stack, Junho 5, 2025.

  1. Stack

    Stack Membro Participativo

    For my assignment, I need to import baseball salary data into a pandas DataFrame.
    From there, one of my objectives is to get the salaries of all the teams per year.

    I was successful however in order to move onto the next task, I need a pandas DataFrame. sumofSalaries.dtype is returning int64.

    Questions:
    1. How do I convert the data in the code following into a DataFrame?
    2. How do I delete the indexes in sumofSalaries?

    Code:

    import pandas as pd
    salariesData = pd.read_csv('Salaries.csv')

    #sum salaries by year and team
    sumOfSalaries = salariesData.groupby(by=['yearID','teamID'])['salary'].sum()

    del sumOfSalaries.index.names #line giving me errors

    #create DataFrame from grouped data
    df = pd.DataFrame(sumOfSalaries, columns = ['yearID', 'teamID', 'salary'])
    df

    _____________________________________________________________________________

    sumofSalaries:
    yearID teamID
    1985 ATL 14807000
    BAL 11560712
    BOS 10897560
    CAL 14427894
    CHA 9846178

    ...and so on
    _____________________________________________________________________________

    df:

    yearID teamID salary
    yearID teamID
    1985 ATL NaN NaN 14807000
    BAL NaN NaN 11560712
    BOS NaN NaN 10897560
    CAL NaN NaN 14427894

    Continue reading...

Compartilhe esta Página