I need to automate a table in python that does some simple things. The table has 4 columns (TRAVEL_AGENT, DATE, ROOM_TYPE, ROOM_PRICE). I need a simple script that asks the user for the travel agent, room type and room price and start and end date. I'm stuck at using a date range to insert rows to the table.
Eg. I need dates from 1/4 till 10/5
Travel_Agent | Date | Room_Type | Room_Price |
---|---|---|---|
A | 1/4/22 | DBL | 30 |
A | 2/4/22 | DBL | 30 |
A | 10/5/22 | DBL | 30 |
CodePudding user response:
You can use date_range
, something like this:
import pandas as pd
df = pd.DataFrame(
data={"Date": pd.date_range(start="4/1/2022", end="5/10/2022", freq="d"), "Room_Type": "DBL", "Room_Price": 30, "Travel_Agent": "A"}
)
That will initialize the data as you have above (assuming the date format is inverted)
output:
Date Room_Type Room_Price Travel_Agent
0 2022-04-01 DBL 30 A
1 2022-04-02 DBL 30 A
2 2022-04-03 DBL 30 A
3 2022-04-04 DBL 30 A
4 2022-04-05 DBL 30 A
...
CodePudding user response:
Is this what you are trying to accomplish?
name = input('TRAVEL_AGENT')
room_type = input('ROOM_TYPE')
room_price = input('ROOM_PRICE')
date_start = input('Date Start YYYY-MM-DD')
date_end = input('Date End YYYY-MM-DD')
df = pd.DataFrame({
'TRAVEL_AGENT' : [name, name],
'ROOM_TYPE' : [room_type, room_type],
'ROOM_PRICE' : [room_price, room_price],
'DATE' : [date_start, date_end]
})
df = (df.set_index('DATE').groupby(['TRAVEL_AGENT', 'ROOM_TYPE', 'ROOM_PRICE']).apply(lambda x: x.asfreq('d', fill_value=0))).drop(['TRAVEL_AGENT', 'ROOM_TYPE', 'ROOM_PRICE'], axis = 1).reset_index()
df