pandas DataFrame and pandas.groupby to calculate Salaries
<p>For my assignment, I need to import baseball salary data into a pandas <code>DataFrame</code>.<br>
From there, one of my objectives is to get the salaries of all the teams per year.</p>

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

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

<p>Code:</p>

<pre><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
</code></pre>

<p><code>del</code> has a <a href="https://docs.python.org/3/reference/simple_stmts.html#del" rel="nofollow">very specific meaning</a> in python and has no use on a dataframe like that.</p>

<p>You want to use <code>reset_index</code> to get rid of the <code>MultiIndex</code> after a groupby -- if you want to get rid of the <code>MultiIndex</code>, that is.</p>

<pre><code>import pandas as pd
salariesData = pd.read_csv('Salaries.csv')

#sum salaries by year and team
sumOfSalaries = (pd.DataFrame(
                 salariesData.groupby(by=['yearID','teamID'])['salary'].sum()
                 .reset_index()
               ))
</code></pre>

<p>Read up on the <a href="http://pandas.pydata.org/pandas-docs/stable/groupby.html" rel="nofollow">groupby docs</a> and the <a href="http://pandas.pydata.org/pandas-docs/stable/advanced.html" rel="nofollow">multiindexing docs</a> for more information. </p>

python pandas dataframe ipython