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_x | col2_x | col1_y | col2_y | |
---|---|---|---|---|
0 | 15 | 93 | 10 | 93 |
1 | 20 | 24 | 20 | 24 |
2 | 136 | 51 | 30 | 52 |
3 | 93 | 22 | 40 | 246 |
4 | 743 | 38 | 50 | 142 |
5 | 60 | 53 | 60 | 53 |
6 | 70 | 72 | 70 | 94 |
7 | 91 | 2 | 80 | 2 |
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
How to Use Python to Get the System Hostname
How to Combine Two Lists into a Dictionary in Python
Equivalent of Numpy.Argsort() in Basic Python
How to Make a Barplot and a Lineplot in the Same Seaborn Plot with Different Y Axes Nicely
How to Remove the First Item from a List
How to Access a File's Properties on Windows
Python:2D Contour Plot from 3 Lists:X, Y and Rho
How to Represent an Infinite Number in Python
Pandas Groupby Without Turning Grouped by Column into Index
Extension Method for Python Built-In Types
Differencebetween an Opencv Bgr Image and Its Reverse Version Rgb Image[:,:,::-1]
How to Add Conda Environment to Jupyter Lab
How Does Keras Calculate the Accuracy
Python 'If X Is Not None' or 'If Not X Is None'
How to Remove the Top and Right Axis in Matplotlib
How to Compare Two JSON Objects with the Same Elements in a Different Order Equal