Kim Minseo
Kim Minseo

Reputation: 81

Add a row to pandas DataFrame with preloaded data

I have a DataFrame that looks something like this:

LeaveID EmployeeID Department      LeaveStartDate LeaveDuration Reason       Status   Remark
1       123474     Sales           2021-01-02     2             Annual Leave Pending 
2       123873     Human Resources 2021-01-02     2             Urgent Leave Approved Family Emergency
...
...

LeaveID is the index column of the data. I wish to append a row to this DataFrame using predetermined variables such as:

row = {
    'EmployeeID' : employeeID,
    'Department' : department,
    'LeaveStartDate' : startDate,
    'LeaveDuration' : duration,
    'Reason' : reason,
    'Status' : status,
    'Remark' : remark
}

How can I add a row to this DataFrame so that its index is automatically 1 larger than the current end of the DataFrame's row?

Upvotes: 1

Views: 103

Answers (1)

tdy
tdy

Reputation: 41327

You can use a similar strategy to wwii's link, but use df.index.max()+1 instead of len(df):

df.loc[df.index.max()+1] = row

Upvotes: 1

Related Questions