【问题标题】:Add a row to pandas DataFrame with preloaded data使用预加载的数据向 pandas DataFrame 添加一行
【发布时间】:2023-03-17 14:25:01
【问题描述】:

我有一个看起来像这样的 DataFrame:

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 是数据的索引列。 我希望使用预先确定的变量向此 DataFrame 追加一行,例如:

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

如何向此 DataFrame 添加一行,使其索引自动比 DataFrame 行的当前末尾大 1?

【问题讨论】:

标签: python pandas


【解决方案1】:

你可以使用类似二战链接的策略,但使用df.index.max()+1而不是len(df)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-15
    • 1970-01-01
    • 2019-08-12
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 2018-08-13
    相关资源
    最近更新 更多