Informatics Practices

Assume that required libraries (Pandas and Numpy) are imported and DataFrame ndf has been created as shown in solved problem 16. Predict the output produced by following code fragment :

print(ndf[ndf["age"] > 30])
print(ndf.head(2)) 
print(ndf.tail(3))

The following DataFrame ndf is from solved problem 16 :

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

Python Pandas

3 Likes

Answer

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

Working

  1. print(ndf[ndf["age"] > 30]) — The ndf["age"] > 30 creates a boolean mask where True indicates rows where the "age" column has values greater than 30. Then ndf[...] uses this boolean mask to filter the DataFrame ndf, returning only rows where the condition is True.

  2. print(ndf.head(2)) — It prints the first 2 rows of the DataFrame ndf.

  3. print(ndf.tail(3)) — It prints the last 3 rows of the DataFrame ndf.

Answered By

3 Likes


Related Questions