[英]Find first 'True' value in blocks in pandas data frame
我有一個數據框,其中一列僅在塊中包含True或False值。 例如:
df =
b
0 False
1 True
2 True
3 False
4 True
5 True
6 True
7 True
8 False
9 False
10 False
11 False
12 False
13 True
14 True
15 True
我需要找到True塊的開頭:
>> find_first_true(df)
>> array([1, 4, 13])
有什么優雅的解決方案嗎?
編輯
感謝您提出的解決方案。 我想知道,從我發現的索引開始提取特定長度的塊的最簡單方法是什么?
例如,我需要在索引之前取長度為4的塊(行數)。 因此,如果我的索引(先前找到)
index = array([1, 4, 13])
然后我需要塊:
[df.loc[0:4], df.loc[9:13]]
要么
b
0 False
1 True
2 True
3 False
4 True
9 False
10 False
11 False
12 False
13 True
我正在遍歷索引,但想知道更多的熊貓解決方案
In [2]: df = pd.read_clipboard()
In [3]: df
Out[3]:
b
0 False
1 True
2 True
3 False
4 True
5 True
6 True
7 True
8 False
9 False
10 False
11 False
12 False
13 True
14 True
15 True
In [11]: np.where(((df.b != df.b.shift(1)) & df.b).values)[0]
Out[11]: array([ 1, 4, 13], dtype=int64)
def find_first_true(df):
#finds indexes of true elements
a = list(map(lambda e: e[0] + 1 if e[1] else 0, enumerate(df)))
a = list(filter(bool, a))
a = list(map(lambda x: x - 1, a))
#removes consecutive elements
ta = [a[0]] + list(filter(lambda x: a[x] - a[x-1] != 1, range(1, len(a))))
a = list(map(lambda x: a[x], ta))
return a
find_first = []
for i in range(len(df)):
if (df.loc[i, 'b'] == False and df.loc[i+1, 'b'] == True):
find_first.append(i+1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.