def clean_df(df, fill_nan=True, drop_empty_columns=True):
"""Clean a pandas dataframe by:
1. Filling empty values with Nan
2. Dropping columns with all empty values
Args:
df: Pandas DataFrame
fill_nan (bool): If any empty values (strings, None, etc) should be replaced with NaN
drop_empty_columns (bool): If columns whose values are all empty should be dropped
Returns:
DataFrame: cleaned DataFrame
"""
if fill_nan:
df = df.fillna(value=np.nan)
if drop_empty_columns:
df = df.dropna(axis=1, how='all')
return df.sort_index()
评论列表
文章目录