问题陈述:
我有一个数据框,如下所示:
Description Type x y z
0 Branch Actuated 0 0 0
1 Forward Actuated 7.07 7.07 0
2 Backwards Actuated 7.07 -2.93 0
3 Forward Actuated 17.07 -2.93 0
4 Backwards Actuated 10 -10 0
5 Forward Actuated 17.07 -17.07 0
6 EOL Actuated 7.07 -17.07 0
7 Forward Actuated -7.07 -7.07 0
8 Backwards Actuated -7.07 2.93 0
9 Forward Actuated -17.07 2.93 0
10 Backwards Actuated -10 10 0
11 Forward Actuated -17.07 17.07 0
12 EOL Actuated -7.07 17.07 0
13 Forward Actuated -0 10 0
14 Forward Actuated -0 20 0
15 Forward Actuated 10 0 0
遇到EOL
行时,算法需要反向查找第一行Branch
,并将其插入EOL
ro之后,如下所示:
Description Type x y z
0 Branch Actuated 0 0 0
1 Forward Actuated 7.07 7.07 0
2 Backwards Actuated 7.07 -2.93 0
3 Forward Actuated 17.07 -2.93 0
4 Backwards Actuated 10 -10 0
5 Forward Actuated 17.07 -17.07 0
6 EOL Actuated 7.07 -17.07 0
0 Branch Actuated 0 0 0 <--
7 Forward Actuated -7.07 -7.07 0
8 Backwards Actuated -7.07 2.93 0
9 Forward Actuated -17.07 2.93 0
10 Backwards Actuated -10 10 0
11 Forward Actuated -17.07 17.07 0
12 EOL Actuated -7.07 17.07 0
0 Branch Actuated 0 0 0 <--
13 Forward Actuated -0 10 0
14 Forward Actuated -0 20 0
15 Forward Actuated 10 0 0
注意:反向搜索应基于原始数据框而不是增强的数据框。
问题: 如何有效地(资源松懈和快速)做到这一点?
答案 0 :(得分:1)
使用merge_asof
获取最后匹配的行,将concat
,DataFrame.sort_index
和最后DataFrame.reset_index
与drop=True
连接在一起,以防止索引值重复:>
df1 = df[df['Description'] == 'Branch']
print (df1)
Description Type x y z
0 Branch Actuated 0.0 0.0 0
df2 = df[df['Description'] == 'EOL']
print (df2)
Description Type x y z
6 EOL Actuated 7.07 -17.07 0
12 EOL Actuated -7.07 17.07 0
df3=pd.merge_asof(df2,df1,left_index=True,right_index=True,suffixes=('_',''))[df1.columns]
print (df3)
Description Type x y z
6 Branch Actuated 0.0 0.0 0
12 Branch Actuated 0.0 0.0 0
df = pd.concat([df, df3]).sort_index().reset_index(drop=True)
print (df)
Description Type x y z
0 Branch Actuated 0.00 0.00 0
1 Forward Actuated 7.07 7.07 0
2 Backwards Actuated 7.07 -2.93 0
3 Forward Actuated 17.07 -2.93 0
4 Backwards Actuated 10.00 -10.00 0
5 Forward Actuated 17.07 -17.07 0
6 EOL Actuated 7.07 -17.07 0
7 Branch Actuated 0.00 0.00 0
8 Forward Actuated -7.07 -7.07 0
9 Backwards Actuated -7.07 2.93 0
10 Forward Actuated -17.07 2.93 0
11 Backwards Actuated -10.00 10.00 0
12 Forward Actuated -17.07 17.07 0
13 EOL Actuated -7.07 17.07 0
14 Branch Actuated 0.00 0.00 0
15 Forward Actuated -0.00 10.00 0
16 Forward Actuated -0.00 20.00 0
17 Forward Actuated 10.00 0.00 0