Pandas Dataframe Fillna() Only Some Columns in Place

Pandas dataframe fillna() only some columns in place

You can select your desired columns and do it by assignment:

df[['a', 'b']] = df[['a','b']].fillna(value=0)

The resulting output is as expected:

     a    b    c
0 1.0 4.0 NaN
1 2.0 5.0 NaN
2 3.0 0.0 7.0
3 0.0 6.0 8.0

Pandas how to fillna in place on a column?

Ed Chum's comment's correctly points out the difference between the methods you propoosed. Here is an example I used to show how it works.

import pandas as pd
import numpy as np

d = {'col1': [1, 2, 3, 4], 'col2': [3, 4, np.nan, np.nan]}
df = pd.DataFrame(data=d)

df
   col1  col2
0 1 3.0
1 2 4.0
2 3 NaN
3 4 NaN
df['col2'].fillna(value=6, inplace=True)
   col1  col2
0 1 3.0
1 2 4.0
2 3 6.0
3 4 6.0

Having posted this, I think it'd be most valuable to see what your my_value variable's value is and what your dataframe looks like.

I discard Aditya's hypothesis. In the case the nan would be a string, it would appear between quotations marks, and it doesn't.

Hope this helps!

Fillna in multiple columns in place in Python Pandas

You could use apply for your columns with checking dtype whether it's numeric or not by checking dtype.kind:

res = df.apply(lambda x: x.fillna(0) if x.dtype.kind in 'biufc' else x.fillna('.'))

print(res)
A B City Name
0 1.0 0.25 Seattle Jack
1 2.1 0.00 SF Sue
2 0.0 0.00 LA .
3 4.7 4.00 OC Bob
4 5.6 12.20 . Alice
5 6.8 14.40 . John

fillna of more than one column by using column numbers instead of names

Just reporting the answer that mozway correctly suggested in the comments (all creds to him)

The solution is simply

df.iloc[:,1:50] = df.iloc[:,1:50].fillna(100)

meaning that you want to select every row : and columns between 1 and 50 1:50. Beware that selection is exclusive on the second index.

forward fill specific columns in pandas dataframe

tl;dr:

cols = ['X', 'Y']
df.loc[:,cols] = df.loc[:,cols].ffill()

And I have also added a self containing example:

>>> import pandas as pd
>>> import numpy as np
>>>
>>> ## create dataframe
... ts1 = [0, 1, np.nan, np.nan, np.nan, np.nan]
>>> ts2 = [0, 2, np.nan, 3, np.nan, np.nan]
>>> d = {'X': ts1, 'Y': ts2, 'Z': ts2}
>>> df = pd.DataFrame(data=d)
>>> print(df.head())
X Y Z
0 0 0 0
1 1 2 2
2 NaN NaN NaN
3 NaN 3 3
4 NaN NaN NaN
>>>
>>> ## apply forward fill
... cols = ['X', 'Y']
>>> df.loc[:,cols] = df.loc[:,cols].ffill()
>>> print(df.head())
X Y Z
0 0 0 0
1 1 2 2
2 1 2 NaN
3 1 3 3
4 1 3 NaN

Pandas fillna multiple columns with values from corresponding columns without repeating for each

  • you can use **kwargs to assign()
  • build up a dict with a comprehension to build **kwargs
import pandas as pd
import numpy as np
x = pd.DataFrame({'col1_x': [15, np.nan, 136, 93, 743, np.nan, np.nan, 91] ,
'col2_x': [np.nan, np.nan, 51, 22, 38, np.nan, 72, np.nan],
'col1_y': [10, 20, 30, 40, 50, 60, 70, 80],
'col2_y': [93, 24, 52, 246, 142, 53, 94, 2]})

x.assign(**{c:x[c].fillna(x[c.replace("_x","_y")]) for c in x.columns if "_x" in c})





































































col1_xcol2_xcol1_ycol2_y
015931093
120242024
2136513052
3932240246
47433850142
560536053
670727094
7912802

Fillna is not working in pandas DataFrame

inplace fillna with multiple columns does not work for some reasons https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

you can do without inplace like this

dframe[['A', 'B']] = dframe[['A', 'B']].fillna(0.0)

or you can fillna all columns

dframe = dframe.fillna(0.0)

How to apply fillna to last N columns of a pandas dataframe?

A recipe that should work here is

df.iloc[:, -x:] = df.iloc[:, -x:].fillna(value=0)

A reproducible example here is

import pandas as pd
df = pd.DataFrame({'col1':range(10),
'col2':range(1, 11),
'col3':range(2, 12),
'col4':range(3, 13),
'col5':range(4, 14)})

# pepper with NaNs
df.iloc[8, 2] = None
df.iloc[8, 3] = None
df.iloc[8, 4] = None

# apply fillna change to last 2 cols
x = 2
df.iloc[:, -x:] = df.iloc[:, -x:].fillna(value=0)

print(df)


Related Topics



Leave a reply



Submit