Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import libraries
- import re
- import pandas as pd
- import numpy as np
- # create a dataframe
- df = pd.DataFrame({"columnA1": np.linspace(0, 10, num=10),
- "columnB20": np.linspace(10, 20, num=10),
- "columnC305": np.linspace(20, 30, num=10)}
- )
- # view dataframe columns names
- print(df.columns)
- # create replacement column names without trailing numbers
- new_cols = (re.sub(r"\d*$", "", col) for col in df.columns)
- rename_cols = dict(zip(df.columns, new_cols))
- # rename columns
- df = df.rename(columns=rename_cols)
- print(df.columns)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement