Q. Write code statements to list the following, from a dataframe namely sales.


(a) List only columns 'Item' and 'Revenue'.

(b) List rows from 3 to 7.

(c) List the value of cell in 5th row, 'Item' column.


Answer = 

#(A)  
sales['Item'] 
sales["Revenue"]

#(B) 
sales.loc[3:7]

#(c) 
sales['item'][4]

Explaination :-

[ ] is used to select a column by mentioning the respective column name.

To extract/list multiple columns syntax is Dataframe[['column_name','column_name','column_name'...]]

(a) sales[['Item','Revenue']]

Using Dataframe.iloc[ ]

iloc[ ] is used for selection based on position. It is similar to loc[] indexer but it takes only integer values to make selections.

To select multiple rows syntax DataFrame.iloc[[1, 2, 3]]

(b) sales.iloc[[2,3,4,5,6]]

(c) sales['item'][4]


10 Comments

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

Post a Comment

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

Previous Post Next Post