パンダ:マージ行は、私はこのようになりますパンダのデータフレーム持っているデータフレーム
df =pd.DataFrame([[0,10,0,'A','A',6,7],[11,21,1,'A','A',8,9],[0,13,1,'B','B',11,13],[0,12,1,'C','C',14,15],[13,14,0,'C','C',16,18]],columns=['Start Sample','End Sample','Value','Start Name','End Name','Start Time','End Time'])
df
Out[18]:
Start Sample End Sample Value Start Name End Name Start Time End Time
0 0 10 0 A A 6 7
1 11 21 1 A A 8 9
2 0 13 1 B B 11 13
3 0 12 1 C C 14 15
4 13 14 0 C C 16 18
が、私はグループと同じValue
を持つ連続した行をしたいのであれば、行i
の行i+1
と終了時間の開始時間の間の差< 3
たとえば、1,2,3行は同じ値を持つ連続する行です。
df['Start Time'].iloc[2] - df['End Time'].iloc[1] is = 2
df['Start Time'].iloc[3] - df['End Time'].iloc[2] is = 1
だから、それらはすべてマージする必要があります。 私はこれらの行になることを希望:
df2
Out[25]:
Start Sample End Sample Value Start Name End Name Start Time End Time
0 0 10 0 A A 6 7
1 11 12 1 A C 8 15
2 13 14 0 C C 16 18
マージされた新しい行が持つべきであることに注意してください:
1) Start Sample = to the Start Sample of the first row merged
2) End Sample = to the End Sample of the last row merged
3) Value = to the common value
4) Start Name = to the Start Name of the first row merged
5) End Name = to the End Name of the last row merged
6) Start Time = to the Start Name of the first row merged
7) End Name = to the End Name of the last row merged
こんにちは、ありがとうございます。しかし、行iの開始時間と行iの終了時間の差が<3である場合にのみ行をグループ化したいと思います。この条件をどこに追加できますか? – gabboshow
@ gabboshow oops。そのロジックで編集を追加しました。ブーリアンをミックスして一致させると、変更を見つけて、クマをグループ化します。 –