As3adTintin
As3adTintin

Reputation: 2476

python replace string function throws asterix wildcard error

When i use * i receive the error

raise error, v # invalid expression error: nothing to repeat

other wildcard characters such as ^ work fine.

the line of code: df.columns = df.columns.str.replace('*agriculture', 'agri')

am using pandas and python

edit: when I try using / to escape, the wildcard does not work as i intend

In[44]df = pd.DataFrame(columns=['agriculture', 'dfad agriculture df'])

In[45]df
Out[45]: 
Empty DataFrame
Columns: [agriculture, dfad agriculture df]
Index: []

in[46]df.columns.str.replace('/*agriculture*','agri')
Out[46]: Index([u'agri', u'dfad agri df'], dtype='object')

I thought the wildcard should output Index([u'agri', u'agri'], dtype='object)


edit: I am currently using hierarchical columns and would like to only replace agri for that specific level (level = 2).

original:

df.columns[0] = ('grand total', '2005', 'agriculture')
df.columns[1] = ('grand total', '2005', 'other')

desired:

df.columns[0] = ('grand total', '2005', 'agri')
df.columns[1] = ('grand total', '2005', 'other')

I'm looking at this link right now: Changing columns names in Pandas with hierarchical columns

and that author says it will get easier at 0.15.0 so I am hoping there are more recent updated solutions

Upvotes: 2

Views: 417

Answers (1)

EdChum
EdChum

Reputation: 394159

You need to the asterisk * at the end in order to match the string 0 or more times, see the docs:

In [287]:
df = pd.DataFrame(columns=['agriculture'])
df

Out[287]:
Empty DataFrame
Columns: [agriculture]
Index: []

In [289]:
df.columns.str.replace('agriculture*', 'agri')

Out[289]:
Index(['agri'], dtype='object')

EDIT

Based on your new and actual requirements, you can use str.contains to find matches and then use this to build a dict to map the old against new names and then call rename:

In [307]:
matching_cols = df.columns[df.columns.str.contains('agriculture')]
df.rename(columns = dict(zip(matching_cols, ['agri'] * len(matching_cols))))

Out[307]:
Empty DataFrame
Columns: [agri, agri]
Index: []

Upvotes: 2

Related Questions