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]
Very good site
ReplyDeleteThank you : )
DeleteAds are very disturbing
ReplyDeleteSorry !
Deletecan u explain more while giving the answers!!!
ReplyDeleteOk.
DeleteHelped a lot thanks for the effort
ReplyDeleteWelcome : )
DeleteCan we write sales.loc[:,'Item', 'revenue'] for option a?
ReplyDeleteno
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )