我正在尋找一種解決方案來在某些條件改變時獲得行。
這是我的資料框的示例。
ts fdw time_stamp
0 n [0, 0] 1635211605896
1 n [0, 0] 1635211606896
2 l [0, 0] 1635211607896
3 l [0, 0] 1635211608896
4 l [0, 0] 1635211609896
5 l [0, 0] 1635211609896
6 n [0, 0] 1635211609896
在上述資料幀,我想提取行時列名ts被改變,例如n以l或l以n。
這是我的預期輸出。
ts fdw time_stamp
1 n [0, 0] 1635211606896
2 l [0, 0] 1635211607896
5 l [0, 0] 1635211609896
6 n [0, 0] 1635211609896
uj5u.com熱心網友回復:
import pandas
import pdrle
# Data
df = pandas.DataFrame({"ts": ["n", "n", "l", "l", "l", "l", "n"]})
df["val"] = [1, 2, 3, 4, 5, 6, 7]
# Get runs of consecutive lengths in ts
rle = pdrle.encode(df.ts)
grp = rle.index.repeat(rle.runs)
# Get first and last row of each runs
ans = (
df.groupby(grp)
.apply(lambda x: x.iloc[[-1], :] if len(x) == 1 else x.iloc[[0, -1], :])
.droplevel(0)
)
# If the first and last group have more than two rows, remove duplicates
if rle.runs.iloc[0] > 1:
ans.drop(ans.head(1).index, inplace=True)
if rle.runs.iloc[-1] > 1:
ans.drop(ans.tail(1).index, inplace=True)
ans
# ts val
# 1 n 2
# 2 l 3
# 5 l 6
# 6 n 7
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/343218.html
