0

I would like to build a 3,000 row long dataframe with code only (so far I import the document from XLS), following these rules:

Top index: enter image description here

Flight Number: I would like to define the numbers in another simpler dataframe. The flight numbers stay the same 24 times (see departure times). Dataframe would look like this:

df = pd.DataFrame({'Flight Number':['LX2104','LX2105','LX2320','LX2321','LX1232','LX1232'], 
               'Leg Route': ['GVA-AGP','GVA-AGP','GVA-AJA','GVA-AJA','GVA-ARN','GVA-ARN']})
               'Leg Flight Pair': ['LX2104/2105','LX2104/2105','LX2320/2321','LX2320/2321','LX1232/1233','LX1232/1233']})

enter image description here

STD Departure: From - to 23 (-, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23) Leg Route: Similar to Flight Number this would also be defined in the dataframe (df above) Leg Flight Pair: Similar to Flight Number & Leg Route, this would also be defined in the dataframe (df above) Products: I would list the product names in a list

This would be the beginning of the expected outcomeenter image description here

2
  • Hello @ManuH. Just to clarify, you want 4 columns, 3 of them will all be the same value apart from STD Departure which will range from 0 to 23? Commented Feb 25, 2019 at 14:14
  • Yes, first four columns are most important. 3 of them will be defined in another dataframe, however, I still need them 24 times each. Commented Feb 25, 2019 at 14:31

1 Answer 1

3
df = pd.DataFrame({
    'Flight Number':['LX2104','LX2105','LX2320','LX2321','LX1232','LX1232'], 
    'Leg Route': ['GVA-AGP','GVA-AGP','GVA-AJA','GVA-AJA','GVA-ARN','GVA-ARN'],
    'Leg Flight Pair': ['LX2104/2105','LX2104/2105','LX2320/2321','LX2320/2321','LX1232/1233','LX1232/1233']
})
n_flights = len(df)
df = pd.concat([df]*24).sort_values('Flight Number').reset_index(drop=True)
df['STD Departure'] = np.tile(np.arange(24), n_flights)
cols = ['Prod1', 'Prod2']
for col in cols:
  df[col] = 0

Output

>>> df.head()

Flight Number   Leg Flight Pair     Leg Route   STD Departure   Prod1   Prod2
0   LX1232      LX1232/1233         GVA-ARN             0           0       0
1   LX1232      LX1232/1233         GVA-ARN             1           0       0
2   LX1232      LX1232/1233         GVA-ARN             2           0       0
3   LX1232      LX1232/1233         GVA-ARN             3           0       0
4   LX1232      LX1232/1233         GVA-ARN             4           0       0
Sign up to request clarification or add additional context in comments.

1 Comment

(Sorry for the delay) It worked! Thank you very much. Is there a way that I can add the product column headers from a dictionary or another dataframe? This way I don't have to always change it in the script but can change the dictionary/dataframe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.