本文介绍了根据pandas DataFrame中的值序列生成索引元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
It's a follow up to my previous question here: Finding the index of rows based on a sequence of values in a column of pandas DataFrame
我想获取一个索引非常差的元组列表,然后是第一次出现的坏"索引:
I want to get a list of tuples that has index of very bad, followed with the the index of first occurrence of 'bad':
import random
df = pd.DataFrame({
'measure': [random.randint(0,10) for _ in range(0,20)],
})
df['status'] = df.apply(
lambda x: 'good' if x['measure'] > 4 else 'very bad' if x['measure'] < 2 else 'bad',
axis=1)
这是数据框:
measure status
0 8 good
1 8 good
2 0 very bad
3 5 good
4 2 bad
5 3 bad
6 9 good
7 9 good
8 10 good
9 5 good
10 1 very bad
11 7 good
12 7 good
13 6 good
14 5 good
15 10 good
16 3 bad
17 0 very bad
18 3 bad
如何获取这样的组合的元组?
How can I get a tuple of such combinations?
[(2,4),(10,16),(17,18)]
[(2,4), (10,16), (17,18)]
IIUC,您可以尝试:
IIUC, you can try:
# filters only rows with bad and very bad
m = df[df['status'].isin(['bad','very bad'])]
# check id current row is very bad and next row is bad
c = m['status'].eq('very bad') & m['status'].shift(-1).eq('bad')
# if true return next row as true too and get only index values
idx = m[c|c.shift()].index
# convert every 2 items into a tuple
res = [*zip(idx[::2],idx[1::2])]
[(2, 4), (10, 16), (17, 18)]
这篇关于根据pandas DataFrame中的值序列生成索引元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!