Create a dataframe by using the dataset [tipsnew.csv] and perform the following operations a) Print the day value when the value of total_bill is maximum and minimum. b) Retrieve all the rows from dataframe when the value of tip is more than 3.0 and less than 5.0 c) Print the time of the meal and gender of the person who paid the tip more than 5.0 d) Select and display last 10 rows from the dataframe when customer smoking habbit is ‘yes’ e) Create the newdataframe(ndf) from original dataframe(df) containing all rows and the following columns [total_bill tip, size]. Sort and display all values of column tip in ascendending order.
import pandas as pd
df = pd.read_csv(r"tipsnew.csv")
dataframe = pd.DataFrame(df)
# Print the day value when the value of total_bill is maximum and minimum.
total_bill = dataframe['total_bill']
total_bill_max = total_bill.max()
total_bill_min = total_bill.min()
index_max = dataframe.index[dataframe['total_bill'] == total_bill_max].tolist()
index_min = dataframe.index[dataframe['total_bill'] == total_bill_min].tolist()
print('\n a)')
print('Day maximum: ', dataframe.loc[index_max[0]]['day'])
print('Day minimum: ', dataframe.loc[index_min[0]]['day'])
# All the rows from dataframe when the value of tip is more
# than 3.0 and less than 5.0
b = dataframe[(dataframe.tip < 5.0) & (dataframe.tip > 3.0)]
print('\n b)')
print(b[['total_bill', 'tip', 'sex', 'smoker', 'day', 'time', 'size']])
# The time of the meal and gender of the person who paid the tip more than 5.0\
c = dataframe[dataframe.tip > 5.0]
print('\n c)')
print(c[['sex', 'tip']])
# Last 10 rows from the dataframe when customersmoking habbit is ‘yes’
d = dataframe[dataframe.smoker == 'Yes']
print('\n d)')
print(d[-10:])
# newdataframe from original dataframe
# containing all rows and the following columns [total_bill tip, size].
# Sort and display all values of column tip in ascendending order.
newdataframe = dataframe[['total_bill', 'tip', 'size']]
print('\n e)')
print(newdataframe
Comments
Leave a comment