Unpredictably formatted df
:
First Name number last_name
0 Cthulhu 666 Smith
df = pd.DataFrame({'First Name': ['Cthulhu'], 'number': [666], 'last_name': ['Smith']})
This needs to be put into column names and order: TemplateColumns = ['First Name', 'other', 'number']
. If columns don't exist they can be created:
for col in TemplateColumns:
if col not in df:
df[col] = np.nan
Which gives:
First Name number last_name other
0 Cthulhu 666 Smith NaN
And initial columns need to be ordered the same as TemplateColumns
, leaving the remaining columns at the end, to get desired_df
:
First Name other number last_name
0 Cthulhu NaN 666 Smith
desired_df = pd.DataFrame({'First Name': ['Cthulhu'], 'other': [np.nan], 'number': [666], 'last_name': ['Smith']})
Reordering columns is well explained in other posts, but I don't know how to order the first n columns and keep the rest at the end. How can I do this?