I would like to remove the prefix from all column names in a dataframe.
I tried creating a udf and calling it in a for loop
def remove_prefix(str, prefix):
if str.startswith(blabla):
return str[len(prefix):]
return str
for x in df.columns:
x.remove_prefix()
To remove prefix from column labels in Pandas DataFrame, use the str. lstrip(~) method.
You can use sub in base R to remove "m" from the beginning of the column names.
drop_duplicates(). T you can drop/remove/delete duplicate columns with the same name or a different name. This method removes all columns of the same name beside the first occurrence of the column also removes columns that have the same data with the different column name.
Use Series.str.replace
with regex ^
for match start of string:
df = pd.DataFrame(columns=['pre_A', 'pre_B', 'pre_predmet'])
df.columns = df.columns.str.replace('^pre_', '')
print (df)
Empty DataFrame
Columns: [A, B, predmet]
Index: []
Another solution is use list comprehension with re.sub
:
import re
df.columns = [re.sub('^pre_',"", x) for x in df.columns]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With