In: Computer Science
The following is a preview of the dataframe products:
item cost profit sold_out 1 A 46 15 FALSE 2 B 50 5 TRUE 3 C 28 10 FALSE 4 D 38 12 FALSE 5 E 20 5 TRUE
Write the code that returns the items with profit equal or greater than 12:
Output: item cost profit sold_out 1 A 46 15 FALSE 4 D 38 12 FALSE
import pandas as pd
# creating products dictionary
data = {
'item': ['A' ,'B' ,'C' ,'D' ,'E'],
'cost': [46 ,50 ,28 ,38 ,20],
'profit': [15 ,5 ,10 ,12 ,5],
'sold_out': [False ,True ,False ,False ,True]
}
# converting data dictionary to dataframe
products = pd.DataFrame(data)
# actual indexes start from 0
# we have to increase by 1
products.index = products.index + 1
print("Preview of products : \n")
# printing dataframe
print(products)
print("------------------------------------\n")
print("Products with Profit >= 12 : \n")
# products.profit >= 12
# checks all the profits and returns a list of
# True and False based on that outer products will display
# whole data of products with profit >= 12
print(products[products.profit>=12])
Code :
Output :