Using a for loop in Python dataframe to count rows == 1

I have a dataframe (I'll call it df here). For certain columns, I am trying to loop over them and do simple operations (namely, count the number of rows with a value == 1 and calculate a percentage). The code I wrote is below:

cols = ['column1', 'column2', 'column3', 'column4', 

for x in cols:
    count = df[df[x]==1].x.count()
    percent = round(count/123 * 100, 1)
    print(x + count + percent)

I'm getting an error on the line I define the count variable. Any help is appreciated.

AttributeError                            Traceback (most recent call last)
<ipython-input-48-8ca715023110> in <module>()
      7 for x in cols:
----> 8     count = df[df[x]==1].x.count()
      9     percent = round(count/123 * 100, 1)
     10     print(x + count + percent)

/usr/lib64/python3.4/site-packages/pandas/core/ in __getattr__(self, name)
   2742             if name in self._info_axis:
   2743                 return self[name]
-> 2744             return object.__getattribute__(self, name)
   2746     def __setattr__(self, name, value):

AttributeError: 'DataFrame' object has no attribute 'x'

Read more here:

Content Attribution

This content was originally published by mountainave at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: