Let’s filter
the numeric columns then using get_loc
obtain the location in the dataframe correspoding to the start and end location of the numeric columns, finally use reindex
with fill_value=0
to reindex accordingly:
c = dd.filter(regex=r'^\d+$').columns
l1, l2 = dd.columns.get_loc(c[0]), dd.columns.get_loc(c[-1])
idx = np.hstack([dd.columns[:l1], np.r_[c.astype(int).min():c.astype(int).max() + 1].astype(str), dd.columns[l2 + 1:]])
dd = dd.reindex(idx, axis=1, fill_value=0)
a 1 2 3 4 5 6 b
0 1 1 1 0 1 0 1 1
CLICK HERE to find out more related problems solutions.