SNIPPET
1 Upvote

Converting Values to NaNs in All Columns

Python
Data Preprocessing

Example of how to impute data in all columns. In this example we are replacing any values that are question marks to NaNs.

import pandas as pd
import numpy as np

#If value in dataframe is ? then return NaN, otherwise return the value unchanged.
def impute_data(x):
    if x == "?":
        return np.NaN
    else:
        return x

for col, content in data.items():
    data[col] = data[col].apply(impute_data)

By GregHe1979 - Last Updated Dec. 12, 2020, 4:37 p.m.

COMMENTS
RELATED SNIPPETS
Pivoting Pandas Dataframes
Python
Data Preprocessing

Pandas

3
SparkSession instantiate
Python
Data Preprocessing

2
Converting Data Types
Python
Data Preprocessing

Data-cleaning

2
Data Minification
Python
Data Preprocessing

Cleaning

2
Search Snippets by Tag: