我有一个包含 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
实现这一目标的最佳方法是什么?
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
这是一种方法:
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
另一种可能的解决方案:
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