I have a pandas DataFrame like this
import pandas as pd
import numpy as np
data = {
'col1': [0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, np.nan, np.nan, np.nan],
'col2': [1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0],
'col3': [1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0]
}
df = pd.DataFrame(data)
print(df)
# col1 col2 col3
# 0 0.0 1.0 1.0
# 1 1.0 1.0 1.0
# 2 1.0 1.0 0.0
# 3 1.0 1.0 1.0
# 4 1.0 1.0 1.0
# 5 0.0 0.0 0.0
# 6 1.0 0.0 0.0
# 7 1.0 0.0 0.0
# 8 0.0 0.0 0.0
# 9 0.0 1.0 1.0
# 10 0.0 1.0 1.0
# 11 NaN 1.0 0.0
# 12 NaN 1.0 1.0
# 13 NaN 1.0 1.0
How can I find the columns that have 4 or more consecutive 1.0
?
In my example, col1
and col2
are what I want to find.
Because df['col1'][1:5]
contains 4 1.0
s and df['col2'][:5]
contains 5 1.0
s.