<jupyter_start><jupyter_text>04 - Grouping  Step 1. Import the necessary libraries<jupyter_code>import pandas as pd<jupyter_output><empty_output><jupyter_text>Step 2. Create the DataFrame with the following values:<jupyter_code>raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'], 
        'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'], 
        'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'], 
        'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],
        'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}
regiment = pd.DataFrame(raw_data)
regiment<jupyter_output><empty_output><jupyter_text>Step 3. Assign it to a variable called regiment.<jupyter_code>regiment<jupyter_output><empty_output><jupyter_text>Step 4. What is the mean preTestScore from the regiment Nighthawks?<jupyter_code>def filter_func(x):
    return x['regiment'] == 'Nighthawks'
display(regiment, regiment.groupby('regiment').mean(), regiment.groupby('regiment').filter(filter_func))<jupyter_output><empty_output><jupyter_text>Step 5. Present general statistics by company<jupyter_code>regiment.groupby('company').describe()<jupyter_output><empty_output><jupyter_text>Step 6. What is the mean of each company's preTestScore?<jupyter_code>regiment.groupby('company')['postTestScore'].mean()<jupyter_output><empty_output><jupyter_text>Step 7. Present the mean preTestScores grouped by regiment and company<jupyter_code>regiment.groupby(['company', 'regiment'])['postTestScore'].mean()<jupyter_output><empty_output><jupyter_text>Step 8. Present the mean preTestScores grouped by regiment and company without heirarchical indexing  Step 9. Group the entire dataframe by regiment and company<jupyter_code>regiment.set_index(['regiment', 'company'])<jupyter_output><empty_output><jupyter_text>Step 10. What is the number of observations in each regiment and company<jupyter_code>regiment.set_index(['regiment', 'company']).count
regiment['regiment'] = regiment.apply(count(), axis = 0)
list(range(len(regiment.index)))
#no me sale<jupyter_output><empty_output><jupyter_text>Step 11. Iterate over a group and print the name and the whole data from the regiment<jupyter_code>#muy perdido aqui ya<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup