Python Tutorial: Pandas DataFrame Exercises

Friday, 30 October 2020

Pandas DataFrame Exercises


1.    Write code to create a Series object using the Python sequence [11,21,31,41]. Assume that Pandas is imported as alias name pd.

Solution:

import pandas as pd

s1=pd.Series([11,21,31,41])

print(“Series object 1”)

print(s1)

Output

Series object 1

0    11

1    21

2    31

3    41

dtype: int64

 

2.    Write code to create a Series object using the Python sequence (1,2,3,4). Assume that Pandas is imported as alias name pd.

Solution:

import pandas as pd

s1=pd.Series((1,2,3,4))

print(“Series object 1”)

print(s1)

Output

Series object 1

0    1

1    2

2    3

3    4

dtype: int64

 

3.    Write a program to create a Series object using individual characters ‘o’,’h’, and ‘o’. Assume that Pandas is imported alias name pd.

Solution:

import pandas as pd

print("Series object")

s4=pd.Series(['o','h','o'])

print(s4)

Output

Series object

0    o

1    h

2    o

dtype: object

 

4.    Write a program to create a Series object using a string: “So funny”. Assume that Pandas is imported as alias name pd.

Solution:

            import pandas as pd

print("Series object")

s4=pd.Series("So funny")

print(s4)

Output

Series object

0    So funny

dtype: object

 

5.    Write a program to create a Series object using three different words : “I”, “am”, “laughing”.Assume that Pandas is imported as alias name pd.

Solution:

import pandas as pd

print("Series object")

s5=pd.Series(["I", "am", "laughing"])

print(s5)

Output

Series object

0           I

1          am

2    laughing

dtype: object

 

6.    Write a program to create a Series object using an ndarray that has 5 elements in the ranges 24 to 64.

Solution:

import pandas as pd

import numpy as np

s6=pd.Series(np.linspace(24,64,5))

print(s6)

Output

0    24.0

1    34.0

2    44.0

3    54.0

4    64.0

dtype: float64

 

7.    Write a program to create a Series object using an ndarray that is created by tiling a list [3,5],  twice.

Solution:

import pandas as pd

import numpy as np

s7=pd.Series(np.tile([3,5],2))

print(s7)

Output

0    3

1    5

2    3

3    5

dtype: int32

 

8.    Write a program to create a Series object using a dictionary that stores the number of students in each section of class 12 in your school.

Solution:

                  import pandas as pd

import numpy as np

stu={'Vanya':65,'Sanya':87,'Manya':89}

s8=pd.Series(stu)

print(s8)

Output

Vanya    65

Sanya    87

Manya    89

dtype: int64

 

9.    Write a program to create a Series object that stores initial budget allocated (50000/- each) for the four quarters of the years: Qtr1, Qtr2, Qtr3 and Qtr4.

Solution:

import pandas as pd

s9=pd.Series(50000,index=['Qtr1','Qtr2','Qtr3','Qtr4'])

print(s9)

Output

Qtr1    50000

Qtr2    50000

Qtr3    50000

Qtr4    50000

dtype: int64

 

10.Total number of medals to be won is 200 in the Inter University games held every alternate year. Write code to create a Series object that these medals for games to be held in the decade 2020-2029.

Solution:

import pandas as pd

s10=pd.Series(200, index=range(2020,2029,2))

print(s10)

Output

2020    200

2022    200

2024    200

2026    200

2028    200

dtype: int64

 

11.A Python list namely section stores the section names (‘A’, ‘B’, ‘C’, ‘D’) of class 12 in your school. Another list contri stores the contribution made by these students to a charity fund endorsed by the school. Write code to create a Series object that stores the contribution amount as the values and the section names as the indexes.

Solution:

import pandas as pd

section=['A','B','C','D']

contri=[6700,5600,5000,5200]

s11=pd.Series(data=contri,index=section)

print(s11)

Output

A    6700

B    5600

C    5000

D    5200

dtype: int64

 

12.Sequence section and contri1 store the section names (‘A’, ‘B’, ‘C’, ‘D’) and contribution made by them respectively (6700, 5600, 5000, 5200, nil) for a charity. Your school has decided to donate as much contribution as made by each section, i.e. the donation will be doubled.

Solution :

import pandas as pd

import numpy as np

section=['A','B','C','D','E']

contri1=np.array([6700,5600,5000,5200,np.NaN])

s12=pd.Series(data=contri1*2,index=section, dtype=np.float32)

print(s12)

Output

A    13400.0

B    11200.0

C    10000.0

D    10400.0

E        NaN

dtype: float32

 

13.Consider the rows series objects s11 and s12 that you created in examples 11 and 12 respectively. Print the attributes of both these objects in a report form as shown below:

Attribute name                                Object s11                Object s12

Data type

Shape

No. of bytes

No. of dimensions

Item size

Has NaNs?

Empty?

Solution:

 

14.Consider a Series object s8 that stores the number of students in each section of class 12 (as shown below)

Vanya    65

Sanya    87

Manya    89

Solution:

import pandas as pd

stu={'Vanya':65,'Sanya':87,'Manya':89}

s8=pd.Series(stu)

print("Tickets amount :")

print(s8[:2]*100)

Output

Tickets amount :

Vanya    6500

Sanya    8700

dtype: int64

 

15.Consider the Series object s13 that stores the contribution of each section, as shown below:

Solution:

                  A    13400.0

B    11200.0

C    10000.0

D    10400.0

E        NaN

Solution:

import pandas as pd

import numpy as np

section=['A','B','C','D','E']

contri1=np.array([6700,5600,5000,5200,np.NaN])

s12=pd.Series(data=contri1*2,index=section, dtype=np.float32)

print(s12)

s12[0]=7600

s12[3:]=7000

print("Series object after modifying amounts ")

print(s12)

16.A Series object trdata  consist of around 2500 rows of data. Write a program to print the following details :

(i)           First 100 rows of data                   (ii) Last 5 rows of data

Solution:

          import pandas as pd

          print(trdata.head(100))

          print(trdata.tail())

17.Number of students in classes 11 and 12 in three streams (‘Science’, ‘Commerce’, ‘Humanities’) are stored in two Series objects c11 and 12. Write code to find total number of students in classes 11 and 12, stream wise.

Solution:

import pandas as pd

import numpy as np

c11=pd.Series(data=[30,40,50],index=['Science', 'Commerce', 'Humanities'])

c12=pd.Series(data=[37,44,45],index=['Science', 'Commerce', 'Humanities'])

print("Total no. of students")

print(c11+c12)

Output

Total no. of students

Science       67

Commerce      84

Humanities    95

dtype: int64

 

18.Object1 Population stores the details of population in four metro cities of India and Object2 AvgIncome stores the total average income reported in previous year in each of these metros. Calculate income per capita for each of these metro cities.

Solution:

import pandas as pd

Population=pd.Series([21435432,32435432,21325465,21436576]),\

 index=[‘Delhi’, ‘Mumbai’, ‘Kolkata’, ‘Chennai’])

AvgIncome=pd.Series([32436587437632,21435467984321,43215487094321,21325476874321],\

index=[‘Delhi’, ‘Mumbai’, ‘Kolkata’, ‘Chennai’])

perCapita=AvgIncome/Population

print(“Population in for metro cities ”)

print(Population)

print(“Avg. Income in four metro cities”)

print(AvgIncome)

print(“Per Capita Income in four metro cities”)

print(perCapita)

Output:

Population in for metro cities

Delhi      21435432

Mumbai     32435432

Kolkata    21325465

Chennai    21436576

dtype: int64

Avg. Income in four metro cities

Delhi      32436587437632

Mumbai     21435467984321

Kolkata    43215487094321

Chennai    21325476874321

dtype: int64

Per Capita Income in four metro cities

Delhi      1.513223e+06

Mumbai     6.608658e+05

Kolkata    2.026473e+06

Chennai    9.948173e+05

dtype: float64

19.What will be the output produced by the following program?

import pandas as pd

info=pd.Series(data=[31,41,51])

print(info)

print(info>40)

print(info[infor>40])

Solution:

 

 

20.Series object s11 stores the charity contribution made by each section (see below):

A        6700

B        5600

C        5000

D        5200

Write a program to display which section made a contribution more than Rs. 5500/-

Solution:

          import pandas as pd

          print(“Contribution>5000 by :”)

          print(s11[s11>5500])

 

21.Given a dictionary that stores the section names’  list as value for ‘Section’ key and contribution amounts’ list as value for ‘Contri’ key:

Dict1={‘Section’:[‘A’, ‘B’, ‘C’, ‘D’], ‘Contri’:[6700,5600, 5000, 5200]}

Write code to create and display the data frame using above dictionary.

Solution:

import pandas as pd

Dict1={‘Section’:[‘A’, ‘B’, ‘C’, ‘D’], ‘Contri’:[6700,5600, 5000, 5200]}

Df1=pd.DataFrame(Dict1)

print(df1)    

22.Create and display a DataFrame from a 2D dictionary, Sales, which stores the quarter-wise sales as inner dictionary for two years, as show below:

Sales={‘yr1’:{‘Qtr1’:34500, ‘Qtr2’:56000, ‘Qtr3’:47000, ‘Qtr4’:49000 },

          {‘yr2’: {‘Qtr1’:47500, ‘Qtr2’:46100, ‘Qtr3’:56000, ‘Qtr4’:59000 }}

Solution:

import  pandas as pd

Sales={‘yr1’:{‘Qtr1’:34500, ‘Qtr2’:56000, ‘Qtr3’:47000, ‘Qtr4’:49000 },

          {‘yr2’: {‘Qtr1’:47500, ‘Qtr2’:46100, ‘Qtr3’:56000, ‘Qtr4’:59000 }}

dfSales=pd.DataFrame(Sales)

print(dfSales)

23.Carefully read the following code.

import pandas as pd

yr1={‘Qtr1’ :44900, ‘Qtr2’: 46100, ‘Q3’:57000, ‘Q4’:59000}

yr2={‘A’:54500, ‘B’ :51000, ‘Qtr4’:57000}

disSales1={1:yr1, 2:yr2}

df3=pd.DataFrame(diSales1)

(i)           List the index labels of the DataFrame df3

(ii)          List the column names of DataFrame df3.

Solution :

(i)           The index labels of df3 will include : A, B, Q3, Q4, Qtr1, Qtr2, Qtr4.

The total number of indexes  is equal total unique inner keys, i.e., 7.

(ii)          The column name of df3 will be: 1,2

24.Write a program to create a dataframe from a list containing dictionaries of the sales performance of four zonal offices. Zones names should be the should be the row labels.

Solution:

import pandas as pd

zoneA={‘Target’:56000, ‘Sales’:58000}

zoneB={‘Target’:70000, ‘Sales’:68000}

zoneC={‘Target’:75000, ‘Sales’:78000}

zoneD={‘Target’:60000, ‘Sales’:61000}

sales=[zoneA, zoneB, zoneC,  zoneD]

salesDf=pd.DataFrame(Sales, index=[‘zoneA’, ‘zoneB’, ‘zoneC’,  ‘zoneD’])

print(saleDf)

25.Write a program to create a dataframe from a 2D list. Specify own index labels.

Solution:

Import pandas as pd

List2=[[25,45,60],[34,67,89],[88,90,56])

Df2=pd.DataFrame(List2, index=[‘row1’, ‘row2’, ‘row3’])

print(Df2)

26.Write a program to create a dataframe from a list containing 2 lists, each contain Target and actual Sales figures of four zonal offices. Give appropriate row labels.

Solution:

          Import pandas as pd

          Target=[50000,56000,43000,78000]

          Sales=[58000, 68000, 78000, 61000]

          ZoneSales=[ Target, Sales]

          zsaleDf=pd.DataFrame(ZoneSales, columns=[[‘zoneA’, ‘zoneB’, ‘zoneC’,  ‘zoneD’], \

          index=[‘ Target’, ‘Sales’])

          print(zsalesDf)

27.What will be the output of following code?

import pandas as pd

import numpy as np

Arr1=np.array([[11,12],[13,14],[15,16]], np.int32)

Dtf2=pd.DataFrame(arr1)

Solution:

0              1

0        11      12

1        13      14

2        15      16

28.What a program to create a DataFrame from a 2D array as shown below:

             101        113    124

        130        140    200

        115        216    217

Solution:

import pandas as pd

import numpy as np

arr2=np.array([[101,113,124],[130,140,200],[115,216,217]])

df=pd.DataFrame(arr2)

print(df)

Output:

          0          1    2

0        101  113  124

1        130  140  200

2        115  216  217

 

29. Consider two series objects staff and salaries that store the number of people in various office branches and salaries distributed in these branches, respectively.

Write a program to create another Series object that stores average salary per branch and then create a DataFrame object from these Series objects.

Solution:

import pandas as pd

import numpy as np

staff=pd.Series([20,36,44])

salaries=pd.Series([234000,542000,763000])

avg=salaries/staff

org={'people':staff,'Amount':salaries, 'Average':avg}

df=pd.DataFrame(org)

print(df)

Output:

 people  Amount       Average

0      20  234000  11700.000000

1      36  542000  15055.555556

2      44  763000  17340.909091

 

30. Write a program to create a DataFrame to store weight, age and names of 3 people. Print the DataFrame and its transpose.

Solution:

import pandas as pd

import numpy as np

df=pd.DataFrame({'Weight':[43,65,76],\

                 'Name':['Amit','Aviral','Ankit'],\

                 'Age':[30,19,21]})

print('Original DataFrame')

print(df)

print('Transepose')

print(df.T)

Output:

Original DataFrame

   Weight    Name  Age

0      43    Amit   30

1      65  Aviral   19

2      76   Ankit   21

Transepose

           0       1      2

Weight    43      65     76

Name    Amit  Aviral  Ankit

Age       30      19     21

 

 

31.Given a DataFrame namely aid that stores the aid by NGOs for different states:

 

Toys

Books

Uniform

Shoes

Andhra

7912

6532

986

8765

Odisha

9854

5432

543

7654

M.P.

5465

4356

342

6787

U.P.

8798

9867

768

9865

Write a program to display the aid for

(i)                   Books and Uniform only

(ii)                  Shoes only

Solution:

import pandas as pd     

aid=pd.DataFrame({'Toys':[7912,       6532,  986,    8765,],\

                    'Books':[9854,     5432,  543,    7654],\

                    'Uniform':[5465,   4356,  342,    6787],\

                    'Shoes':[8798,     9867,  768,    9865]}, \

                   index=['Andhra','Odisha','M.P.','U.P.'])

print("Aid for books and uniform")

print(aid[['Books','Uniform']])

print('Aid for shoes:')

print(aid.Shoes)

Output :

Aid for books and uniform

                    Books  Uniform

Andhra          9854     5465

Odisha          5432     4356

M.P.              543      342

U.P.              7654     6787

Aid for shoes:

Andhra    8798

Odisha    9867

M.P.       768

U.P.      9865

Name: Shoes, dtype: int64

 

32.Given a DataFrame namely aid that stores the aid by NGOs for different states:

 

Toys

Books

Uniform

Shoes

Andhra

7912

6532

986

8765

Odisha

9854

5432

543

7654

M.P.

5465

4356

342

6787

U.P.

8798

9867

768

9865

Write a program to display the aid for states ‘Andhra’ and ‘Odisha’ for Books and Uniform only.

Solution:

import pandas as pd

aid=pd.DataFrame({'Toys':[7912,   6532,  986,    8765,],\

                    'Books':[9854, 5432,  543,    7654],\

                    'Uniform':[5465,       4356,  342,    6787],\

                    'Shoes':[8798, 9867,  768,    9865]}, \

                   index=['Andhra','Odisha','M.P.','U.P.'])

print(aid.loc['Andhra':'Odisha','Books':'Uniform'])

Output:

          Books  Uniform

Andhra   9854     5465

Odisha   5432     4356

 

33. Consider the following dataframe saleDf:

Target           Sales

          zoneA           56000           58000

          zoneB           70000           68000

          zoneC           75000           78000

          zoneD           60000           61000

write a program to add a column namely Orders having values 6000,6700,6000 respectively for the zones A,B,C and D. The program should also add a new for a new zone zoneE. Add some dummy values in this row.

Solution:

import pandas as pd

saleDf=pd.DataFrame({'Target ':[56000,70000,75000,60000],\

'Sales':[58000,68000,78000,61000]},\

                                index=['zoneA','zoneB','zoneC','zoneD'])

saleDf['Orders']=[6000,6700,6200,6000]

saleDf.loc['zoneE',:]=[50000,45000,5000]

print(saleDf)

Output:

          Target     Sales  Orders

zoneA  56000.0  58000.0  6000.0

zoneB  70000.0  68000.0  6700.0

zoneC  75000.0  78000.0  6200.0

zoneD  60000.0  61000.0  6000.0

zoneE  50000.0  45000.0  5000.0

 

 

34.From the df used above, create another DataFrame and it must not contain the column ‘Population’ and the row Bangalore.

          Population  Hospital  Schools

Delhi             21436576       324     4322

Mumbai         21436587       657     5432

Kolkata          76437632       245     6732

Chennai         43763254       763     3467

Banglore        98658798       879     4356

Solution:

import pandas as pd

Adict={'Population':[21436576,21436587,76437632,43763254,98658798],'Hospital':[324,657,245,763,879],\

       'Schools':[4322,5432,6732,3467,4356]}

df=pd.DataFrame(Adict,columns=['Population','Hospital','Schools'],index=['Delhi','Mumbai','Kolkata','Chennai','Banglore'])

del df['Population']

df1=df.drop(['Banglore'])

print(df1)

Output:

                    Hospital  Schools

Delhi             324                 4322

Mumbai        657           5432

Kolkata          245           6732

Chennai         763           3467

 

35.Consider the following dataframe saleDf:

Target           Sales

          zoneA           56000           58000

          zoneB           70000           68000

          zoneC           75000           78000

          zoneD           60000           61000

write a program to rename indexes of ‘zoneC’, and ‘zoneD’ as ‘Central’ and ‘Daksin’ respectively and the column names ‘Target’ and ‘Sales’ as ‘Targeted’ and ‘Acieved’ respectively.

Solution:

import pandas as pd

saleDf=pd.DataFrame({'Target ': [56000,70000,75000,60000],\

'Sales':[58000,68000,78000,61000]},\

                              index=['zoneA','zoneB','zoneC','zoneD'])

saleDf['Orders']=[6000,6700,6200,6000]

print(saleDf.rename(index={'zoneC':'Central','zoneD':'Daksin'},\

                    columns={'Target':'Targeted','Sales':'Acieved'}))

Output:

               Target   Acieved  Orders

zoneA      56000    58000    6000

zoneB      70000    68000    6700

Central    75000    78000    6200

Daksin     60000    61000    6000

 

36.Will the program reflect the renamed indexes and columns names in  the dataframe saleDf? Make Changes in the previous program so that dataframe saleDf has the changed indexes and columns.

Solution:

import pandas as pd

saleDf=pd.DataFrame({'Target ':[56000,70000,75000,60000 ],\

          'Sales':[58000,68000,78000,61000]},\

                        index=['zoneA','zoneB','zoneC','zoneD'])

saleDf['Orders']=[6000,6700,6200,6000]

saleDf.rename(index={'zoneC':'Central','zoneD':'Daksin'},\

                    columns={'Target':'Targeted','Sales':'Achieved'},inplace=True)

print(saleDf)

Output:

                 Target   Acieved  Orders

zoneA      56000    58000    6000

zoneB      70000    68000    6700

Central    75000    78000    6200

Daksin     60000    61000    6000

37.Given a Series that stores the area of some states in km2. Given series has been created like this:

Ser1=pd.Series([32542,543,564,76876,54768,87643,768765,546567,548765,7676,4354,767,343,768753])

(a)  Write code to find out the biggest and smallest areas from the given series.

(b)  From Ser1 of areas (given earlier that stores areas of states in km2), find out the areas that are more that 50000 km2.

Solution:

import pandas as pd

import numpy as np

Ser1=pd.Series([32542,543,564,76876,54768,87643,768765,546567,\

                548765,7676,4354,767,343,768753])

print("Top 3 biggest areas are:")

print(Ser1.sort_values().tail(3))

print("3 smallest areas are :")

print(Ser1.sort_values().head(3))

print(Ser1[Ser1>50000])

Output:

Top 3 biggest areas are:

8        548765

13      768753

6        768765

dtype: int64

3 smallest areas are :

12      343

1        543

2        564

dtype: int64

3      76876

4      54768

5      87643

6     768765

7     546567

8     548765

13    768753

dtype: int64

38.Write a program to create a Series object with 6 random integers and having indexes as :[‘p’,’q’,’r’,’s’,’u’,’t’]

Solution:

import pandas as pd

import numpy as np

s=pd.Series(np.random.randint(6),index=['p','q','r','s','u','t'])

print(s)

39. Write a program to create

(a)  data series s1 and then change the indexes of the Series object in any random order.

(b)  To sort the values of a Series object s1 in ascending order of its values and store in into series object s2.

(c)  To sort the values of a Series object s1 in ascending order of its values and store in into series object s3.

Solution:

import pandas as pd

s1=pd.Series(data=[6100,5200,3000,4200,5500,8600])

print("Original Data Series :")

print(s1)

s2=s1.sort_values()

print("Series object s2 :\n",s2)

s3=s1.sort_index(ascending=False)

print("Series object s2 :\n",s3)

Output:

Original Data Series :

0    6100

1    5200

2    3000

3    4200

4    5500

5    8600

dtype: int64

Series object s2 :

 2    3000

3    4200

1    5200

4    5500

0    6100

5    8600

dtype: int64

Series object s2 :

 5    8600

4    5500

3    4200

2    3000

1    5200

0    6100

dtype: int64

40. Given a Series object S4. Write a program to change the values at its 2nd row(index) and 3rd row to 8000.

Solution:

import pandas as pd

s4=pd.Series(data=[6100,5200,3000,4200,5500,8600])

print("Original Data Series :")

print(s4)

s4[1:3]=8000

print("Series object s4 after changing value :\n", s4)

Output:

Original Data Series :

0    6100

1    5200

2    3000

3    4200

4    5500

5    8600

dtype: int64

Series object s4 after changing value :

 0    6100

1    8000

2    8000

3    4200

4    5500

5    8600

dtype: int64

41.Given a Series object s4. Write a program to calculate the cubes of the Series values.

Solution:

import pandas as pd

s4=pd.Series(data=[5,6,9,7])

print("Original Data Series :")

print(s4)

print(" Cubes of s4 values :\n", s4**3)

Output:

Original Data Series :

0    5

1    6

2    9

3    7

dtype: int64

 Cubes of s4 values :

0    125

1    216

2    729

3    343

dtype: int64

42.Given a Series object s5. Write a program to store the squares of the Series values in object s6. Display s6’s values which are >15.

Solution:

import pandas as pd

s5=pd.Series(data=[2,4,6,8])

print("Original Data Series :")

print(s5)

s6=s5*2

print("Values in s6 > 15 :\n", s6[s6>15])

Output:

Original Data Series :

0    2

1    4

2    6

3    8

dtype: int64

Values in s6 > 15 :

 3    16

dtype: int64

43.Write a program to display number of rows and number of column in DataFrame df.

Solution:

import pandas as pd

df=pd.DataFrame({'Weight':[76,43,65,76],\

                 'Name':['Anshu','Amit','Aviral','Ankit'],\

                 'Age':[30,19,21,23]})

print('Original DataFrame')

print(df)

print("No. of rows :", df.shape[0])

print("No. of columns :", df.shape[1])

Output:

Original DataFrame

               Weight      Name        Age

0              76            Anshu       30

1              43            Amit         19

2              65            Aviral        21

3              76            Ankit         23

No. of rows : 4

No. of columns : 3

44.Write a program to display number of rows and number of columns in DataFrame df without using shape attribute. Also display only the Weight of first and third rows.

Solution:

import pandas as pd

df=pd.DataFrame({'Weight':[76,43,65,76],\

                 'Name':['Anshu','Amit','Aviral','Ankit'],\

                 'Age':[30,19,21,23]})

print('Original DataFrame')

print(df)

rows=len(df.axes[0])

cols=len(df.axes[1])

print("No. of rows: ", rows)

print("No. of columns: ", cols)

print("Display only the Weight of first and third rows")

print(df.iloc[[0,2],[2]])

Output:

Original DataFrame

               Weight      Name        Age

0              76            Anshu       30

1              43            Amit         19

2              65            Aviral        21

3              76            Ankit         23

No. of rows : 4

No. of columns : 3

Display only the Weight of first and third rows

   Age

0   30

2   21

45. Consider Dataframe:

DataFrame :- df

Name

Position

City

Age

Sex

Aakash

Manager

Delhi

35

M

Sunakshi

Programmer

Mumbai

37

F

Amitabh

Manager

Kanpur

33

M

Madhuri

Programmer

Mumbai

40

F

Ashish

Manager

Kanpur

27

M

Akshay

Programmer

Kanpur

34

M

Preeti

Programmer

Delhi

26

F

Govinda

Manager

Delhi

30

M

Rani

Manager

Mumbai

28

F

Shilpa

Manager

Kanpur

26

F

Ayushman

Programmer

Delhi

28

M

Write the statement and output for the following (refer to the above DataFrame(df) :

a)      Display Name of Employee

b)      Display Name and Position of Manager.

c)       Display name and city of Employee those age more than 30 and less than or equal 35

d)      Display female employee name.

e)      Display name record those who are not living Kanpur.

f)       Display Name, position and city those who are living Kanpur and female.

Solution:

import pandas as pd

emp={'Name':['Aakash','Sunakshi','Amitabh','Madhuri','Ashish','Akshay','Preeti','Govinda','Rani','Shilpa','Ayushman'],    'Position':['Manager','Programmer','Manager','Programmer','Manager','Programmer','Programmer','Manager','Manager','Manager','Programmer'],

 'City':['Delhi','Mumbai','Kanpur','Mumbai','Kanpur','Kanpur','Delhi','Delhi','Mumbai','Kanpur','Delhi'],

     'Age':[35,37,33,40,27,34,26,30,28,26,28],

     'Sex':['M','F','M','F','M','M','F','M','F','F','M']}

df=pd.DataFrame(emp)

print(df)

print('a)                Display Name of Employee')

print(df['Name'])

print('b)Display Name and Position of Manager.')

print(df[['Name','Position']])

print('c)                Display name and city of Employee those age more than 30 and less than or equal 35')

print(df[['Name','City','Age']] [(df['Age']>30) & (df['Age']<=35)])

print('d)Display female employee name.')

print(df[['Name','Sex']][df['Sex']=='F'])

print('e)Display name record those who are not living Kanpur.')

print(df[['Name','City']][df['City']=='Kanpur'])

print('f)                Display Name, position and city those who are living Kanpur and female.')

print(df[['Name','Position','City']][(df['City']=='Kanpur') & (df['Sex']=='F')])

46 Create a dataframe furniture shown in the following table:

Item

 Material

 Colour

 Price

Sofa

 Wooden

 Maroon

 25000

Dining Table

 Plywood

Yellow

 20000

Chair

 Plastic

 Red

 1500

Sofa

 Stainless Steel

 Silver

 55000

Chair

 Wooden 

 Light Blue

 2500

Dining Table

 Aluminum

 Golden

 65000

a) Display the details of the chair and sofa.

b) Display furniture details which price is more than 25000.

c) Display the furniture details price under 10000.

d) Display alternative rows.

 

import pandas as pd

data = [['Sofa','Wooden','Maroon',25000],

            ['Dining Table','Plywood','Yellow',20000],

            ['Chair','Plastic', 'Red',1500],

            ['Sofa','Stainless Steel', 'Silver',55000],

            ['Chair','Wooden', 'Light Blue',2500],

            ['Dining Table','Aluminum', 'Golden',65000],]

furniture=pd.DataFrame(data,columns=['Item','Material','Colour','Price'])

print(furniture.to_string(index=False))

print(furniture[furniture['Item']=='Sofa'],"n",furniture[furniture['Item']=='Chair'])

print(furniture[furniture['Price']>25000])

print(furniture[furniture['Price']<10000])

print(furniture.iloc[::2])

No comments:

Post a Comment