Introduction to Pandas
Chapter 5: DataFrame operations
Replacing missing values
Instead of removing NA values, you can also replace them with another value of your choice. Use df.fillna()
for this.
Let us replace any NA values for Type 2
with the string "Standard"
.
>>> # Let's get back our original data before dropping the NAs earlier!
>>> df = pd.read_csv("pokemon.csv", index_col="Name")
>>> df["Type 2"].fillna("Standard", inplace=True)
Sanity check. Look - no more missing values!
>>> print(df.isna().sum())
# 0
Type 1 0
Type 2 0
Total 0
HP 0
Attack 0
Defense 0
Sp. Atk 0
Sp. Def 0
Speed 0
Generation 0
Legendary 0
dtype: int64