user3527975
user3527975

Reputation: 1773

Find missing data in pandas dataframe and fill with NA

I have a dataframe in pandas with company name and date as multi-index.

companyname date emp1 emp2 emp3..... emp80

Where emp1, emp2 is the count of phone calls made by emp1 and 2 respectively on that date. Now there are dates when no employee made a call. Means there are rows where all the column values are 0. I want to fill these values by NA. Should I manually write the names of all columns in some function? Any suggestions how to achieve this?

Upvotes: 2

Views: 1796

Answers (1)

Andy Hayden
Andy Hayden

Reputation: 375475

You can check that the entire row is 0 with all:

In [11]: df = pd.DataFrame([[1, 2], [0, 4], [0, 0], [7, 8]])

In [12]: df
Out[12]: 
   0  1
0  1  2
1  0  4
2  0  0
3  7  8

In [13]: (df == 0).all(1)
Out[13]: 
0    False
1    False
2     True
3    False
dtype: bool

Now you can assign all the entries in these rows to NaN using loc:

In [14]: df.loc[(df == 0).all(1)] = np.nan

In [15]: df
Out[15]: 
    0   1
0   1   2
1   0   4
2 NaN NaN
3   7   8

Upvotes: 4

Related Questions