1

I want to populate a pandas dataframe with a integer called delay time. When I do the below, I am getting an error as below also.

train_df = pd.DataFrame("Train_Number", "Start_Station", "Stop_Station", "Delay_Time") 

for i in range(1,25): 
    train_df = train_df.append(i, "Dublin", "Belfast", 0)

Error: TypeError: data type 'Delay_Time' not understood

I have tried changing 0 to int(0), np.zeros(), but neither worked.

What am I doing wrong?

1 Answer 1

1

For performance reason create list of tuples first and then call DataFrame constructor:

L = []
for i in range(1,25): 
   L.append((i, "Dublin", "Belfast", 0))

Or use list comprehension:

L = [(i, "Dublin", "Belfast", 0) for i in range(1,25)]

cols = ["Train_Number", "Start_Station", "Stop_Station", "Delay_Time"]
train_df = pd.DataFrame(L, columns=cols) 

print (train_df)
    Train_Number Start_Station Stop_Station  Delay_Time
0              1        Dublin      Belfast           0
1              2        Dublin      Belfast           0
2              3        Dublin      Belfast           0
3              4        Dublin      Belfast           0
4              5        Dublin      Belfast           0
5              6        Dublin      Belfast           0
6              7        Dublin      Belfast           0
7              8        Dublin      Belfast           0
8              9        Dublin      Belfast           0
9             10        Dublin      Belfast           0
10            11        Dublin      Belfast           0
11            12        Dublin      Belfast           0
12            13        Dublin      Belfast           0
13            14        Dublin      Belfast           0
14            15        Dublin      Belfast           0
15            16        Dublin      Belfast           0
16            17        Dublin      Belfast           0
17            18        Dublin      Belfast           0
18            19        Dublin      Belfast           0
19            20        Dublin      Belfast           0
20            21        Dublin      Belfast           0
21            22        Dublin      Belfast           0
22            23        Dublin      Belfast           0
23            24        Dublin      Belfast           0
    
Sign up to request clarification or add additional context in comments.

Comments

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.