def ffill_cols(df, cols_to_fill_name='Unn'):
"""
Forward fills column names. Propagate last valid column name forward to next invalid column. Works similarly to pandas
ffill().
:param df: pandas Dataframe; Dataframe
:param cols_to_fill_name: str; The name of the columns you would like forward filled. Default is 'Unn' as
the default name pandas gives unnamed columns is 'Unnamed'
:returns: list; List of new column names
"""
cols = df.columns.to_list()
for i, j in enumerate(cols):
if j.startswith(cols_to_fill_name):
cols[i] = cols[i-1]
return cols
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter