How would I count the number of days based on months with zero data?
<p>I'm writing a script in which I read in a csv with several columns and rows. I need the script to total the values in each column for a single row and return which columns have a value of zero for the row. Here's an example of what the data looks like, there are several other columns but these are the columns of interest for my question:</p>

<pre><code>    JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
     0   0   5   5   0   5   5   5   5   0   0   0
</code></pre>

<p>this is what I have so far:</p>

<pre><code>    import pandas as pd
    import os

    os.chdir('C:\\users\\vroland\\desktop\\RR_WMD\\WUdata')

    fout=open("WUinput.csv","a")
    #read water use file
    df=pd.read_csv("WUtest.csv")
    #Header &amp; months with zero values
    cols=df.columns
    #Boolean array of columns with zero values
    bt=df.apply(lambda x: x==0)
    #List months with zero values
    zar=bt.apply(lambda x:list(cols[x.values]),axis=1)
</code></pre>

<p>I've tried a combination of ways including <code>if</code> statements, but i keep getting an error stating my conditional statement is ambiguous so I'm trying another route. So this is what i have now to go along with the block of code above:</p>

<pre><code>   a=30
   b=31
   c=28
   num_days=pd.DataFrame({'JAN':[b],'FEB':[c],'MAR':[b],'APR':[a],'MAY':[b],
                          'JUN':[a],'JUL':[b],'AUG':[b],'SEP':[a],'OCT':[b],
                          'NOV':[a],'DEC':[b]})
</code></pre>

<p>The idea is to use the values returned in <code>zar</code> to look up the appropriate day value in my data frame <code>num_days</code>. Return this value and calculate the total number of days with a value of zero. </p>

<p>Consider the <code>pd.DataFrame</code> <code>df</code></p>

<pre><code>cols = ['JAN', 'FEB', 'MAR', 'APR',
        'MAY', 'JUN', 'JUL', 'AUG',
        'SEP', 'OCT', 'NOV', 'DEC']

df = pd.DataFrame(np.random.randint(0, 3, (10, 12)), columns=cols)
df
</code></pre>

<p><a href="http://i.stack.imgur.com/1iTj9.png" rel="nofollow"><img src="http://i.stack.imgur.com/1iTj9.png" alt="enter image description here"></a></p>

<hr>

<p>I'll use each rows evaluation of <code>row == 0</code> to be a boolean mask on the columns themselves.  Use <code>list</code> to fit nicely back into a <code>pd.Series</code></p>

<pre><code>df.eq(0).apply(lambda x: list(df.columns[x]), 1)

0                   [FEB, MAR, APR, NOV]
1                        [FEB, OCT, NOV]
2              [JAN, APR, AUG, NOV, DEC]
3                        [MAR, APR, SEP]
4                   [MAY, JUN, NOV, DEC]
5                        [APR, AUG, NOV]
6         [MAR, APR, JUN, OCT, NOV, DEC]
7    [JAN, FEB, APR, JUL, OCT, NOV, DEC]
8              [MAY, JUL, AUG, SEP, OCT]
9         [FEB, MAR, APR, JUN, AUG, SEP]
dtype: object
</code></pre>

<hr>

<p>To get the number of days</p>

<pre><code>days_in_month = pd.Series(dict(
        JAN=31, FEB=28, MAR=31,
        APR=30, MAY=31, JUN=30,
        JUL=31, AUG=31, SEP=30,
        OCT=31, NOV=30, DEC=31
    ))

df.eq(0).dot(days_in_month)

0    119
1     89
2    153
3     91
4    122
5     91
6    183
7    212
8    154
9    180
dtype: int64
</code></pre>

python pandas