如何返回子数据框并附加额外的行?

问题描述 投票:0回答:3

我有一个包含 4 列的数据框,

start_time, end_time, quantity_start, quantity_end
。一个例子)

start_time end_time quantity_start quantity_end
0          10       0              1
10         20       1              3
20.        30       3              10

下一行的

start_time
quantity_start
总是等于上一行的
end_time
quantity_end

我需要返回一个仅包含

start_time
quantity_start
的子数据框,但需要包括一个额外的行来说明与
quantity_end
相关联的
end_time
最后一个
end_time

所以在上面的例子中,需要返回的dataframe应该是:

start_time quantity_start
0          0              
10         1              
20.        3              
30         10

实现这一目标的最佳方法是什么?

pandas dataframe
3个回答
2
投票

您可以使用

lreshape
drop_duplicates

out = pd.lreshape(df, {'start_time': ['start_time', 'end_time'],
                       'quantity_start': ['quantity_start', 'quantity_end']}
                  ).drop_duplicates(ignore_index=True)

输出:

   start_time  quantity_start
0           0               0
1          10               1
2          20               3
3          30              10

1
投票

这是一种方法:

out = (
    df.melt(id_vars=["start_time", "end_time"])
           .drop(["variable", "end_time"], axis=1)
           .drop_duplicates(subset="value", keep="first")
           .rename(columns={"value": "quantity_start"})
)

输出:

print(out)

   start_time  quantity_start
0        0.00               0
1       10.00               1
2       20.00               3
5       20.00              10

0
投票

另一种可能的解决方案:

row = pd.DataFrame({
    'start_time': df['end_time'].iat[-1],
    'quantity_start': df['quantity_end'].iat[-1]
}, index=[len(df)])

pd.concat([df, row]).filter(like='start')

输出:

   start_time  quantity_start
0           0               0
1          10               1
2          20               3
3          30              10
© www.soinside.com 2019 - 2024. All rights reserved.