简体   繁体   English

如何遍历值是元组的字典熊猫并找到第一个True和False值

[英]How to iterate through a dictionary pandas where values are tuples, and find first True and False values

I have a dictionary called weeks_adopted where when I run iteritems() and print the value , I get (example of the values for 3 keys, each key is called app_id ). 我有一本名为weeks_adopted的字典,当我运行iteritems()并打印该value ,我得到了(3个键的值的示例,每个键称为app_id )。 The weeks_adopted dict consists of key value pairs where the key is of type <type 'str'> and the value is a <class 'pandas.core.series.Series'> where dtype is bool . 所述weeks_adopted字典由密钥值对,其中键的类型是的<type 'str'>和值是<class 'pandas.core.series.Series'>其中dtypebool Here is one example of one value , where the indices are basically the week referred to (weeks 0-13 of the year in order): 这是一个value示例,其中的索引基本上是所指的星期(按年顺序从0-13周开始):

Name: app_id_str, dtype: bool
0     False
1     False
2     False
3     False
4     False
5     False
6     False
7     False
8     False
9      True
10    False
11    False
12     True
13    False
Name: app_id_str, dtype: bool
0     False
1     False
2     False
3      True
4     False
5     False
6     False
7     False
8     False
9     False
10    False
11    False
12    False
13    False
Name: app_id_str, dtype: bool
0     False
1     False
2     False
3     False
4     False
5     False
6     False
7     False
8     False
9     False
10    False
11     True
12     True
13     True

What I want to do is calculate the number of rows from the first True value right through to the first False value, for each key, obviously accounting for each cases for example in the 3rd tuple you see the first True after the first False . 我想要做的是计算从第一行数True值一直到第一个False值,每个键,显然考虑每个案件,例如在第三元组首先看到的True后的第一个False Basically this is to do with drop out rates - when does a user first see something (True) and then give it up (False). 基本上,这与辍学率有关-用户何时首先看到某物(真),然后放弃(假)。

In the example of the tuples above, the result should be 1, 1 and 3 in terms of adoption rate. 在上面的元组示例中,就采用率而言,结果应为1、1、3。

Here is my current basic method: 这是我目前的基本方法:

for key,value in weeks_adopted.iteritems():
    start= value.index(True)
    end = value.index(False)
    adoption=end-start
    weeks_adopted[key] = adoption

However I get this error even with this method: 但是,即使使用此方法,我也会收到此错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-32-608c4f533e54> in <module>()
     19 for key,value in weeks_adopted.iteritems():
     20     print value
---> 21     start= value.index(True)
     22     end = value.index(False)
     23     adoption=end-start

TypeError: 'Int64Index' object is not callable

In the answer, please could you help me in what other checks I need to be doing to find the first True and first Last value? 在回答中,请您帮我进行其他哪些检查才能找到第一个True和第一个Last值? I am presuming this type of loop is a common one for many situations? 我想这种循环在许多情况下是常见的吗?

you can try this: 您可以尝试以下方法:

def calc_adoption(ts):
    true_index = ts[ts].index
    if len(true_index) == 0:
        return 0
    first_true_index = true_index[0]
    false_index = ts.index.difference(true_index)
    false_index = false_index[false_index > first_true_index]
    if len(false_index) == 0:
        return 14 - first_true_index
    return false_index[0] - first_true_index

adopted_weeks = {k: calc_adoption(v) for k, v in weeks_adopted.iteritems()}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM