此问题是以下问题的后续内容: What is the best way to check if the last rows of a pandas dataframe meet a condition?
但是我在尝试修改提供的答案以满足我的需求时遇到了麻烦。
条件01 =如果singal
的最后五(5)个连续行(包括最后一个)为1,则将返回1。
条件02 =如果singal
的最后三(3)个连续行(包括最后一个)为0,则它将返回0。
条件03 =在第一次遇到CRITERIA 01或CRITERIA 02之前,它将返回nan
。
条件04 =其他所有内容都是检查的最后一个值。
像这样:
index signal check
0 1 nan
1 1 nan
2 1 nan
3 1 nan
4 1 1
5 1 1
6 0 1
7 0 1
8 0 0
9 0 0
10 0 0
11 1 0
12 0 0
13 1 0
14 0 0
15 1 0
16 1 0
17 1 0
18 1 0
19 1 1
我将不胜感激!
谢谢!
答案 0 :(得分:1)
您需要另外一个rolling(3)
,如下所示
m1 = df.rolling(5).sum().eq(5)
m2 = df.eq(0).rolling(3).sum().eq(3)
df['check'] = df[m1 | m2].ffill()
Out[310]:
signal check
index
0 1 NaN
1 1 NaN
2 1 NaN
3 1 NaN
4 1 1.0
5 1 1.0
6 0 1.0
7 0 1.0
8 0 0.0
9 0 0.0
10 0 0.0
11 0 0.0
12 0 0.0
13 1 0.0
14 0 0.0
15 1 0.0
16 1 0.0
17 1 0.0
18 1 0.0
19 1 1.0
遮罩m2
也可以简化为此
m2 = df.rolling(3).sum().eq(0)
答案 1 :(得分:0)
首先选择数据帧的最后5行,然后选择列,然后检查所有结果是否等于某个值(即:对于CRITERIA 1为1)。
<ScrollView
android:layout_above="@id/advancedcontrol_close"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:scrollbarThumbVertical="@color/colorAccent"
android:fadeScrollbars="false"
android:scrollbarSize="4dp"
android:paddingBottom="@dimen/all_medium_margin"
android:paddingTop="@dimen/all_medium_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="ltr"
android:orientation="vertical"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:paddingStart="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/all_small_margin"
android:layout_marginLeft="@dimen/all_small_margin">
<TextView
style="@style/TextView.Wide.RegularAppearanceSmallSize.Title17"
android:textColor="@color/black87"
android:text="@string/all_equalizer"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/all_small_margin"
/>
<TextView
style="@style/TextView.Wide.RegularAppearanceExtraSmallSize.Description15"
android:padding="0dp"
android:id="@+id/equalizer_state_textview"
android:textColor="@color/black60"
android:visibility="@{!viewModel.waitingForEqualizerData && !viewModel.canExecuteEqualizer?View.VISIBLE:View.GONE}"
android:text="@string/advancedcontrol_error"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/all_small_margin"
/>
<FrameLayout
android:clickable="true"
android:id="@+id/equalizer_overlay"
android:layout_width="match_parent"
android:layout_height="400dp">
<com.sonova.mobileapps.userinterface.common.controls.equalizer.EqualizerView
android:clickable="true"
android:id="@+id/view_eq"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@android:color/transparent"
android:enabled="@{viewModel.canExecuteEqualizer}"
app:fillColor="@color/colorAccent35"
app:progressDrawable="@drawable/seekbar_style"
app:thumb="@drawable/seekbar_thumb"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="@{viewModel.waitingForEqualizerData?View.VISIBLE:View.GONE}"
android:background="@color/white80"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/preparing_equalizer_spinner"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginBottom="@dimen/all_large_margin"
android:src="@drawable/all_spinner_animating"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/advancedcontrol_preparing_equalizer"
android:layout_gravity="center"
android:textAlignment="center"/>
</LinearLayout>
</FrameLayout>
...
然后,您可以类似地建立其他条件。