【问题标题】:Whats the fastest way to loop through a DataFrame and count occurrences within the DataFrame whilst some condition is fulfilled (in Python)?在满足某些条件的情况下(在 Python 中),循环遍历 DataFrame 并计算 DataFrame 中出现次数的最快方法是什么?
【发布时间】:2018-03-07 13:40:11
【问题描述】:

我有一个包含两个布尔字段的数据框(如下所示)。

import pandas as pd

d = [{'a1':False, 'a2':False}, {'a1':True, 'a2':False}, {'a1':True, 'a2':False}, {'a1':False, 'a2':False}, {'a1':False, 'a2':True},
     {'a1': False, 'a2': False}, {'a1':False, 'a2':False}, {'a1':True, 'a2':False}, {'a1':False, 'a2':True}, {'a1':False, 'a2':False},]

df = pd.DataFrame(d)
df

Out[1]: 
      a1     a2
0  False  False
1   True  False
2   True  False
3  False  False
4  False   True
5  False  False
6  False  False
7   True  False
8  False   True
9  False  False

我正在努力寻找实现以下目标的最快和最“Pythonic”的方式:

  • 如果 a1==True,则从当前行计算 a2==False 的实例(例如,第 1 行:a1=True,对于第 1 行的三行,a2 为 False)
  • 在 a2==True 的第一个实例中,停止计数(例如第 4 行,计数 = 3)
  • 将 'count' 的值设置为 开始计数的行上的新 df 列 'a3'(例如,第 1 行上的 'a3' = 3)

目标结果集如下。

      a1     a2  a3
0  False  False   0
1   True  False   3
2   True  False   2
3  False  False   0
4  False   True   0
5  False  False   0
6  False  False   0
7   True  False   1
8  False   True   0
9  False  False   0

我一直在尝试使用 for 循环、iterrows 和 while 循环来实现这一点,但到目前为止还没有能够产生一个很好的嵌套组合来提供我想要的结果。任何帮助表示赞赏。如果问题不完全清楚,我深表歉意。

【问题讨论】:

    标签: python pandas for-loop dataframe while-loop


    【解决方案1】:

    这个怎么样:

    df['a3'] = df.apply(lambda x: 0 if not x.a1 else len(df.a2[x.name:df.a2.tolist()[x.name:].index(True)+x.name]), axis=1)
    

    所以,如果 a1 是 False,则写入 0,否则写入从该行到下一个 True 的列表长度。

    【讨论】:

      【解决方案2】:

      这样就可以了:

      df['a3'] = 0
      # loop throught every value of 'a1'
      for i in xrange(df['a1'].__len__()):
          # if 'a1' at position i is 'True'...
          if df['a1'][i] == True:
              count = 0
              # loop over the remaining items in 'a2'
              # remaining: __len__() - i
              # i: position of 'True' value in 'a1'
              for j in xrange(df['a2'].__len__() - i):
                  # if the value of 'a2' is 'False'...
                  if df['a2'][j + i] == False:
                      # count the occurances of 'False' values in a row...
                      count += 1
                  else:
                      # ... if it's not 'False' break the loop
                      break
              # write the number of occurances on the right position (i) in 'a3'
              df['a3'][i] = count
      

      并产生以下输出:

            a1     a2  a3
      0  False  False   0
      1   True  False   3
      2   True  False   2
      3  False  False   0
      4  False   True   0
      5  False  False   0
      6  False  False   0
      7   True  False   1
      8  False   True   0
      9  False  False   0
      

      编辑:在代码中添加 cmets

      【讨论】:

      • 感谢您的回答。我将不得不逐行查看 for/ifs 的嵌套如何产生正确的答案,因为这是我最初想要的。再次感谢。
      • 哦,对不起,我没有很好地解释我的代码...用 cmets 编辑了代码。
      • 谢谢,非常感谢 cmets。
      猜你喜欢
      • 2019-06-09
      • 2016-12-22
      • 2021-08-19
      • 1970-01-01
      • 2011-07-17
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      相关资源
      最近更新 更多