1

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?

Kim Minseo
  • 81
  • 7
  • Which part are you having trouble with? Does [How to append new row to dataframe in pandas?](https://stackoverflow.com/questions/49916371/how-to-append-new-row-to-dataframe-in-pandas) answer your question? – wwii Apr 06 '21 at 19:03
  • [https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.append.html?#pandas-dataframe-append](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.append.html?#pandas-dataframe-append) ?? – wwii Apr 06 '21 at 19:07

1 Answers1

1

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
tdy
  • 36,675
  • 19
  • 86
  • 83