I have a Dataframe data, whose headers contain multiple different suffices. The common feature among all headers is that the suffix begins with ;
Column-A\foo Column-B\bar
some data some data
I’m trying to get the output to look like;
Column-A Column-B
some data some data
I have tried;
data.columns = data.columns.str.replace(r'\$', '')
But this does not work.
Is there a nice way to do this or should I specify the full name of each suffix?
>Solution :
Your regex for finding a backslash and trailing characters isn’t correct.
import pandas
data = pandas.DataFrame(columns=[r'Column-A\foo',r'Column-B\bar'])
data.columns = data.columns.str.replace(r'\\.*', '', regex=True)
print(data)
Output:
Empty DataFrame
Columns: [Column-A, Column-B]
Index: []