![](/img/trans.png)
[英]Insert Blank Row In Python Data frame when value in column changes?
[英]Assigning a flag to a new column 'A' when continuously 5 cells value of existing column 'B' is blank of a data frame in python
你可以试试这个:
import pandas as pd
import numpy as np
df = pd.DataFrame({'Column B':['a','bc','d','','','ef','df','','','','','','fg']})
df['Column A'] = np.where(df['Column B'].groupby((df['Column B'] != df['Column B'].shift()).cumsum()).transform(len) >= 5,'text-1','text-2')
Column B Column A
0 a text-2
1 bc text-2
2 d text-2
3 text-2
4 text-2
5 ef text-2
6 df text-2
7 text-1
8 text-1
9 text-1
10 text-1
11 text-1
12 fg text-2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.