Pandas: fillna only numeric (int or float) columns
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5
--
Chapters
00:00 Pandas: Fillna Only Numeric (Int Or Float) Columns
00:23 Accepted Answer Score 19
00:41 Answer 2 Score 6
01:58 Thank you
--
Full question
https://stackoverflow.com/questions/6010...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas
#avk47
ACCEPTED ANSWER
Score 19
You can select numeric columns and then fillna E.g:
import pandas as pd
df = pd.DataFrame({'a': [1, None] * 3,
                    'b': [True, None] * 3,
                  'c': [1.0, None] * 3})
# select numeric columns
numeric_columns = df.select_dtypes(include=['number']).columns
# fill -1 to all NaN 
df[numeric_columns] = df[numeric_columns].fillna(-1)
# print
print(df)
ANSWER 2
Score 6
It's an old question, however, I discovered that individually filling the columns is faster than the currently chosen answer:
def func(df, value):
    df = df.copy()
    for col in df:
        # select only integer or float dtypes
        if df[col].dtype in ("int", "float"):
            df[col] = df[col].fillna(value)
    return df
 func(df, value=-1) # or df.pipe(func, value=-1)
      a      b        c
0    1.0    True     1.0
1   -1.0    None    -1.0
2    1.0    True     1.0
3   -1.0    None    -1.0
4    1.0    True     1.0
5   -1.0    None    -1.0
comparing speeds the loop returns 470 µs ± 12.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each), while the accepted answer returns 1.57 ms ± 26.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each).
If the dataframe size is increased to 60,000 rows:  pd.concat([df]*10_000, ignore_index=True), the loop returns 1.48 ms ± 79.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) while the chosen answer returns 2.47 ms ± 140 µs per loop (mean ± std. dev. of 7 runs, 100 loops each).
For both instances, the loop is significantly faster than the chosen answer. Also, your mileage may vary. Just some food for thought, especially when trying to wring out more performance.