Eliminate Row: Sathish, an MBA graduate is currently put to a project of Event Management. Soon he started to accept orders and while working on it, he felt it very hard to maintain the records so he decided to purchase a system which helps him to keep track all events and maintain all upcoming and future events.
This requirement has come to your company. Assuming yourself in the development team, write a code to build a system to enter the number of events(n) that the dataset accepts followed by n event details one after the other. While updating event if any field among the mentioned is missing delete the entire row details.
Note:
Column name in the dataset should be as follows
( ‘Sl No’,’EventName’,’EventManager’,’NoOfDates’,’StartDate’,’EndDate’,’HallName’,’HallAddress’,’HallPricePerDay’,’TotalCost’ )
Eliminate Row using Pandas
Input Format:
The First line of the input consists of the number row in the dataset(n).
Next, n lines correspond to the data to be added to the dataset.
Output Format:
The output consists of data in the dataset after dropping the rows which had the missing value in it.
Print “Invalid Input” if the number of records is less than 1.
Sample Input-Output 1:
Enter number records
-2
Invalid Input
Sample Input -Output 2:
Enter number records
8
1 ,Marraige,krishnanand,2,12-08-2005,13-08-2005,krishna mansion,Krishna Mansion Krishnalaya Shringeri,60000,120000
2 ,Birthday Celebration,Akshay,1,14-08-2005,14-08-2005,Akshayalaya,Akshayalaya Akshaya Dama Yogendra Nagar Shimoga,35000,35000
3 ,House Warming,sunil,1,21-10-2005,21-10-2005,Sugamya Corner,Sugamya corner Vijaya nagar Mysore,50000,50000
4 ,Naming Ceremony,Surya,1,10-11-2005,10-11-2005,Suryodaya comforts,Suryodaya comforts Srirampura,20000,
5 ,Promossion Celebration,Aliya,1,15-11-2005,15-11-2005,Kabule convention hall,Kabule convention hall Bangalore,20000,20000
6 ,New Year Celebration,,2,31-12-2005,01-12-2005,Chinnaswammy stadium,Bangalore,,24000
7 ,Chrismas Celebration,Collin,2,24-12-2005,,Saint Philominas chruch,Saint Philominas chruch Mysore,25000,50000
8 ,Project outing,Santhosh,2,29-11-2005,30-11-2005,Mysore Palace Resort,Mysore Palace Resort Mysore,15000,30000
+—+——-+————————+————–+———–+————+————+————————+————————————————–+—————–+———–+
| | Sl No | EventName | EventManager | NoOfDates | StartDate | EndDate | HallName | HallAddress | HallPricePerDay | TotalCost |
+—+——-+————————+————–+———–+————+————+————————+————————————————–+—————–+———–+
| 0 | 1 | Marraige | krishnanand | 2 | 12-08-2005 | 13-08-2005 | krishna mansion | Krishna Mansion Krishnalaya Shringeri | 60000 | 120000 |
| 1 | 2 | Birthday Celebration | Akshay | 1 | 14-08-2005 | 14-08-2005 | Akshayalaya | Akshayalaya Akshaya Dama Yogendra Nagar Shimoga | 35000 | 35000 |
| 2 | 3 | House Warming | sunil | 1 | 21-10-2005 | 21-10-2005 | Sugamya Corner | Sugamya corner Vijaya nagar Mysore | 50000 | 50000 |
| 4 | 5 | Promossion Celebration | Aliya | 1 | 15-11-2005 | 15-11-2005 | Kabule convention hall | Kabule convention hall Bangalore | 20000 | 20000 |
| 7 | 8 | Project outing | Santhosh | 2 | 29-11-2005 | 30-11-2005 | Mysore Palace Resort | Mysore Palace Resort Mysore | 15000 | 30000 |
+—+——-+————————+————–+———–+————+————+————————+————————————————–+—————–+———–+
Additional Sample TestCases
Sample Input and Output 1 :
Enter number records -2 Invalid Input
Sample Input and Output 2 :
Enter number records 8 1 ,Marraige,krishnanand,2,12-08-2005,13-08-2005,krishna mansion,Krishna Mansion Krishnalaya Shringeri,60000,120000 2 ,Birthday Celebration,Akshay,1,14-08-2005,14-08-2005,Akshayalaya,Akshayalaya Akshaya Dama Yogendra Nagar Shimoga,35000,35000 3 ,House Warming,sunil,1,21-10-2005,21-10-2005,Sugamya Corner,Sugamya corner Vijaya nagar Mysore,50000,50000 4 ,Naming Ceremony,Surya,1,10-11-2005,10-11-2005,Suryodaya comforts,Suryodaya comforts Srirampura,20000, 5 ,Promossion Celebration,Aliya,1,15-11-2005,15-11-2005,Kabule convention hall,Kabule convention hall Bangalore,20000,20000 6 ,New Year Celebration,,2,31-12-2005,01-12-2005,Chinnaswammy stadium,Bangalore,,24000 7 ,Chrismas Celebration,Collin,2,24-12-2005,,Saint Philominas chruch,Saint Philominas chruch Mysore,25000,50000 8 ,Project outing,Santhosh,2,29-11-2005,30-11-2005,Mysore Palace Resort,Mysore Palace Resort Mysore,15000,30000 5 10 +---+-------+------------------------+--------------+-----------+------------+------------+------------------------+--------------------------------------------------+-----------------+-----------+ | | Sl No | EventName | EventManager | NoOfDates | StartDate | EndDate | HallName | HallAddress | HallPricePerDay | TotalCost | +---+-------+------------------------+--------------+-----------+------------+------------+------------------------+--------------------------------------------------+-----------------+-----------+ | 0 | 1 | Marraige | krishnanand | 2 | 12-08-2005 | 13-08-2005 | krishna mansion | Krishna Mansion Krishnalaya Shringeri | 60000 | 120000 | | 1 | 2 | Birthday Celebration | Akshay | 1 | 14-08-2005 | 14-08-2005 | Akshayalaya | Akshayalaya Akshaya Dama Yogendra Nagar Shimoga | 35000 | 35000 | | 2 | 3 | House Warming | sunil | 1 | 21-10-2005 | 21-10-2005 | Sugamya Corner | Sugamya corner Vijaya nagar Mysore | 50000 | 50000 | | 4 | 5 | Promossion Celebration | Aliya | 1 | 15-11-2005 | 15-11-2005 | Kabule convention hall | Kabule convention hall Bangalore | 20000 | 20000 | | 7 | 8 | Project outing | Santhosh | 2 | 29-11-2005 | 30-11-2005 | Mysore Palace Resort | Mysore Palace Resort Mysore | 15000 | 30000 | +---+-------+------------------------+--------------+-----------+------------+------------+------------------------+--------------------------------------------------+-----------------+-----------+ Sample Input and Output 3 : Input: 1 7 ,Chrismas Celebration,Collin,2,24-12-2005,,Saint Philominas chruch,Saint Philominas chruch Mysore,25000,50000 Enter number records +--+-------+-----------+--------------+-----------+-----------+---------+----------+-------------+-----------------+-----------+ | | Sl No | EventName | EventManager | NoOfDates | StartDate | EndDate | HallName | HallAddress | HallPricePerDay | TotalCost | +--+-------+-----------+--------------+-----------+-----------+---------+----------+-------------+-----------------+-----------+ +--+-------+-----------+--------------+-----------+-----------+---------+----------+-------------+-----------------+-----------+
Solution:
import pandas as pd
import numpy as np
from tabulate import tabulate
n = int(input("Enter number records\n"))
df = pd.DataFrame(columns=['Sl No', 'EventName', 'EventManager', 'NoOfDates', 'StartDate', 'EndDate', 'HallName', 'HallAddress', 'HallPricePerDay', 'TotalCost'])
if n < 1:
print("Invalid Input")
else:
for i in range(n):
data = input().split(',')
df.loc[i] = data
df = df.replace(r'^\s*$', np.nan, regex=True)
df = df.dropna(axis='rows')
if df.empty:
print("+--+-------+-----------+--------------+-----------+-----------+---------+----------+-------------+-----------------+-----------+")
print("| | Sl No | EventName | EventManager | NoOfDates | StartDate | EndDate | HallName | HallAddress | HallPricePerDay | TotalCost |")
print("+--+-------+-----------+--------------+-----------+-----------+---------+----------+-------------+-----------------+-----------+")
print("+--+-------+-----------+--------------+-----------+-----------+---------+----------+-------------+-----------------+-----------+")
else:
print(tabulate(df, headers='keys', tablefmt='pretty'))
Happy Learning – If you require any further information, feel free to contact me.