vendredi 2 août 2019

python how to count dummy variable in if statement

I have a dataframe "total" including columns like

  • latitude(lat)
  • longtitude(lon)
  • f2(ex ) 1950-12-06, 1959-08-01,...) that represents some day
  • F_chicken(1:chicken, 0: not chicken, dummy variable)

I want to count the number of F_chicken :which some row 'lat' & 'lon' are same and some row 'f2' is smaller.

![][my dataframe.head().]

I try to make this c_chicken column using for loop, but fail...

n = len(total['f2'])
def col_counts(col):
    count = []
    for i,j in range(n):
        if (i != j) and (total['f2'][i] <= total['f2'][j]) and (total['lat'][i]==total['lat'][j]) and (total['lon'][i]==total['lon'][j]) and(col[j] == 1): count[i] += 1
    return count
total['c_chicken'] = col_counts(total.F_chicken)


---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)
<ipython-input-114-879544b4f09a> in <module>
----> 1 total['c_chicken'] = col_counts(total.F_chicken)

<ipython-input-113-ece8cb8d9ef5> in col_counts(col)
      2 def col_counts(col):
      3     count = []
----> 4     for i,j in range(n):
      5         if (i != j) and (total['f2'][i] <= total['f2'][j]) and 
(total['lat'][i]==total['lat'][j]) and (total['lon'][i]==total['lon'][j]) 
and(col[j] == 1): count[i] += 1
      6     return count

TypeError: cannot unpack non-iterable int object

Aucun commentaire:

Enregistrer un commentaire