Q. Consider the DataFrame df  :

       Name Sex    Position       City  Age  Projects  Budget
0     Rabia   F     Manager  Bangalore   30        13      48
1      Evan   M  Programmer  New Delhi   27        17      13
2       Jia   F     Manager    Chennai   32        16      32
3      alit   M     Manager     Mumbai   40        20      21
4  Jaspreet   M  Programmer    Chennai   28        21      17
5      Suji   F  Programmer  Bangalore   32        14      10

Write a program to summaries how much budget is allocated to different Positions, for each City ?


Answer :-

import pandas as pd
import numpy as np

x = { 'Name': ['Rabia', 'Evan', 'Jia', 'alit', 'Jaspreet', 'Suji'],
          'Sex': ['F', 'M', 'F', 'M', 'M', 'F'],
          'Position': ['Manager', 'Programmer', 'Manager', 'Manager',
                       'Programmer', 'Programmer'],\
          'City': ['Bangalore', 'New Delhi', 'Chennai', 'Mumbai',
                   'Chennai', 'Bangalore'],
          'Age': [30, 27, 32, 40, 28, 32],
          'Projects': [13, 17, 16, 20, 21, 14],
          'Budget': [48, 13, 32, 21, 17, 10]}

df = pd.DataFrame (x)

print (df.pivot (index = 'Position', columns = 'City', values= 'Budget'))


Output:-

City        Bangalore  Chennai  Mumbai  New Delhi
Position                                         
Manager          48.0     32.0    21.0        NaN
Programmer       10.0     17.0     NaN       13.0
>>>

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post