pandas fillna mode code example
Example 1: fill na with mode and mean python
cateogry_columns=df.select_dtypes(include=['object']).columns.tolist()
integer_columns=df.select_dtypes(include=['int64','float64']).columns.tolist()
for column in df:
if df[column].isnull().any():
if(column in cateogry_columns):
df[column]=df[column].fillna(df[column].mode()[0])
else:
df[column]=df[column].fillna(df[column].mean)
Example 2: dataframe fillna with 0
df['column'] = df['column'].fillna(0)
Example 3: python pandas fillna
import pandas as pd
nba = pd.read_csv("nba.csv")
nba["College"].fillna("No College", inplace = True)
nba.College.fillna("No College", inplace = True)
print(nba)
Example 4: pandas fillna with mode
data['Native Country'].fillna(data['Native Country'].mode()[0], inplace=True)