我有一个熊猫系列的布尔值,并且我想标记连续的True值组。这怎么可能呢?是否可以矢量化方式进行此操作?任何帮助将不胜感激!
数据:
A
0 False
1 True
2 True
3 True
4 False
5 False
6 True
7 False
8 False
9 True
10 True
所需:
A Label
0 False 0
1 True 1
2 True 1
3 True 1
4 False 0
5 False 0
6 True 2
7 False 0
8 False 0
9 True 3
10 True 3
答案 0 :(得分:4)
这是一个不太可能但简单有效的解决方案:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
//get the text in the edittext first
String name = s.getText().toString();
//then concatenate it with "Mr. " and setText again
s.setText("Mr. " + name);
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {}
});
测试为:
import scipy.ndimage.measurements as mnts
labeled, clusters = mnts.label(df.A.values)
# labeled is what you want, cluster is the number of clusters.
df.Labels = labeled # puts it into df
答案 1 :(得分:2)
cumsum
a = df.A.values
z = np.zeros(a.shape, int)
z[a] = pd.factorize((~a).cumsum()[a])[0] + 1
df.assign(Label=z)
A Label
0 False 0
1 True 1
2 True 1
3 True 1
4 False 0
5 False 0
6 True 2
7 False 0
8 False 0
9 True 3
10 True 3
答案 2 :(得分:1)
您可以使用cumsum
和groupby
+ ngroup
来标记组。
v = (~df.A).cumsum().where(df.A).bfill()
df['Label'] = (
v.groupby(v).ngroup().add(1).where(df.A).fillna(0, downcast='infer'))
df
A Label
0 False 0
1 True 1
2 True 1
3 True 1
4 False 0
5 False 0
6 True 2
7 False 0
8 False 0
9 True 3
10 True 3