简体   繁体   中英

Pandas: Replace dataframe column if it is `NaN`

I have a dataframe with lots of columns. One column may have NaN . In those cases the value can be found in the next column.

To simplify... This here:

In[1]:

d = {'col1': [1, 2, 3], 
     'col2': [5, np.nan, np.nan],
     'col3': [55, 9, 22]}
df = pd.DataFrame(data=d)
print(tabulate(df, headers = 'keys', tablefmt = 'psql'))

Out[1]:

+----+--------+--------+--------+
|    |   col1 |   col2 |   col3 |
|----+--------+--------+--------|
|  0 |      1 |      5 |     55 |
|  1 |      2 |    nan |      9 |
|  2 |      3 |    nan |     22 |
+----+--------+--------+--------+

Should become this here:

In[2]:

d = {'col1': [1, 2, 3], 
     'col2': [5, 9, 22],
     'col3': [55, 9, 22]}
df = pd.DataFrame(data=d)
print(tabulate(df, headers = 'keys', tablefmt = 'psql'))

Out[2]:
+----+--------+--------+--------+
|    |   col1 |   col2 |   col3 |
|----+--------+--------+--------|
|  0 |      1 |      5 |     55 |
|  1 |      2 |      9 | <==  9 |  # col3's value copied to col2
|  2 |      3 |     22 | <== 22 |  # col3's value copied to col2
+----+--------+--------+--------+

I tried this (without success):

df.loc[ df['col2'].isna() ] = df[ df['col2'].isna() ]['col3']

Any advice?

You need a backward fill, row-wise:

df.bfill(axis=1)
#   col1  col2  col3
#0   1.0   5.0  55.0
#1   2.0   9.0   9.0
#2   3.0  22.0  22.0

Yes you can just using np.where

df.col2=np.where(df.col2.isna(),df.col3,df.col2)
df
Out[535]: 
   col1  col2  col3
0     1   5.0    55
1     2   9.0     9
2     3  22.0    22

For fixing your code using .loc

df.loc[ df['col2'].isna(),'col2' ] = df[ df['col2'].isna() ]['col3']
df
Out[538]: 
   col1  col2  col3
0     1   5.0    55
1     2   9.0     9
2     3  22.0    22

也,

d['col2'].fillna(d['col3'], inplace = True)

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