Say I have a dataframe columns as such df.info():
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Action_3.@source 1 non-null object
1 Description_3.#text 1 non-null object
2 Code_3.@source 1 non-null object
3 Others 1 non-null object
4 Animal_1 1 non-null object
To:
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Action.@source_3 1 non-null object
1 Description.#text_3 1 non-null object
2 Code.@source_3 1 non-null object
3 Others 1 non-null object
4 Animal_1 1 non-null object
This need to be done dynamically. My plan is:
- Use regex to find digits in the MIDDLE of string.
- Replace
_{digit}to the back of the column name, iteratively.
My current code :
def check_number_in_column(column_name):
return any(i.isdigit() for i in column_name)
# List out the column names then loop
temp_column_name = temp_df.columns
for j, name in enumerate(temp_column_name):
if check_number_in_column(name) is True :
#TODO