Computer scienceData scienceInstrumentsPandasData analysis with pandas

Crosstab in pandas

Cross-salary

Report a typo

Below is a fragment of the dataset that contains the department, the position, and the salary for each position:

+----+----------------+------------+----------+
|    | department     | position   |   salary |
|----+----------------+------------+----------|
|  0 | R&D            | Manager    |      100 |
|  1 | R&D            | Engineer   |       70 |
|  2 | R&D            | Intern     |       30 |
|  3 | Core           | Manager    |       80 |
|  4 | Core           | Engineer   |       60 |
+----+----------------+------------+----------+

Use pd.crosstab() to generate a table that shows the average salary for each position and each department. Round the values in the resulting dataframe up to the first digit (.round(1)). The output should be in the following format:

+----------------+------------+----------+-----------+---------+
|       position |   Engineer |   Intern |   Manager |     All |
|     department |            |          |           |         |
|----------------+------------+----------+-----------+---------|
| Core           |    60.0    |     25.0 |      80.0 | 55.0    |
| Infrastructure |    90.0    |     35.0 |     120.0 | 81.7    |
| R&D            |    70.0    |     30.0 |     100.0 | 66.7    |
| All            |    73.3    |     30.0 |     100.0 | 67.8    |
+----------------+------------+----------+-----------+---------+
Write a program in Python 3
import pandas as pd

data = pd.DataFrame(
{
'department': ['R&D', 'R&D', 'R&D', 'Core', 'Core', 'Core', 'Infrastructure', 'Infrastructure', 'Infrastructure'],
'position': ['Manager', 'Engineer', 'Intern', 'Manager', 'Engineer', 'Intern', 'Manager', 'Engineer', 'Intern'],
'salary':[100, 70, 30, 80, 60, 25, 120, 90, 35]
}
)

#Your code here

Create a free account to access the full topic