简体   繁体   中英

How to take log of only non-zero values in dataframe and replace O's with NA's?

How do i take log of non-zero values in dataframe and replace 0's with NA's.

I have dataframe like below:

     time                 y1  y2
0    2017-08-06 00:52:00   0   10
1    2017-08-06 00:52:10   1   20
2    2017-08-06 00:52:20   2   0
3    2017-08-06 00:52:30   3   0
4    2017-08-06 00:52:40   0   5
5    2017-08-06 00:52:50   4   6
6    2017-08-06 00:53:00   6   11
7    2017-08-06 00:53:10   7   12
8    2017-08-06 00:53:20   8   0
9    2017-08-06 00:53:30   0   13

I want to take log of all columns expect first column time and log should be calculate of only non-zero values and zero's should be replace with NA's? How do i do this?

So, I tried to do something like this:

cols = df.columns.difference(['time'])
# Replacing O's with NA's using below:

df[cols] = df[cols].mask(np.isclose(df[cols].values, 0), np.nan)

df[cols] = np.log(df[cols]) # but this will try take log of NA's also.

Please help.

Output should be dataframe with same time column, and all the zero's replaced with NA's and log equivalent of the remaining values of all columns expect 1st column.

If I understand correctly, you can just replace the zeros with np.nan and then call np.log directly - it ignores NaN values just fine.

np.log(df[['y1', 'y2']].replace(0, np.nan))

Example

>>> df = pd.DataFrame({'time': pd.date_range('20170101', '20170110'), 
                       'y1' : np.random.randint(0, 3, 10), 
                       'y2': np.random.randint(0, 3, 10)})

>>> df 
        time  y1  y2
0 2017-01-01   1   2
1 2017-01-02   0   1
2 2017-01-03   2   0
3 2017-01-04   0   1
4 2017-01-05   1   0
5 2017-01-06   1   1
6 2017-01-07   2   0
7 2017-01-08   1   0
8 2017-01-09   0   1
9 2017-01-10   2   1

>>> df[['log_y1', 'log_y2']] = np.log(df[['y1', 'y2']].replace(0, np.nan))

>>> df
        time  y1  y2    log_y1    log_y2
0 2017-01-01   1   2  0.000000  0.693147
1 2017-01-02   0   1       NaN  0.000000
2 2017-01-03   2   0  0.693147       NaN
3 2017-01-04   0   1       NaN  0.000000
4 2017-01-05   1   0  0.000000       NaN
5 2017-01-06   1   1  0.000000  0.000000
6 2017-01-07   2   0  0.693147       NaN
7 2017-01-08   1   0  0.000000       NaN
8 2017-01-09   0   1       NaN  0.000000
9 2017-01-10   2   1  0.693147  0.000000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM