【发布时间】:2015-12-14 12:27:03
【问题描述】:
当我使用* 时收到错误消息
raise error, v # invalid expression
error: nothing to repeat
^ 等其他通配符也可以正常工作。
代码行:
df.columns = df.columns.str.replace('*agriculture', 'agri')
我正在使用 pandas 和 python
编辑:
当我尝试使用/ 转义时,通配符无法正常工作
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')
我认为通配符应该输出Index([u'agri', u'agri'], dtype='object)
编辑:
我目前正在使用分层列,并且只想将 agri 替换为该特定级别(级别 = 2)。
原文:
df.columns[0] = ('grand total', '2005', 'agriculture')
df.columns[1] = ('grand total', '2005', 'other')
想要的:
df.columns[0] = ('grand total', '2005', 'agri')
df.columns[1] = ('grand total', '2005', 'other')
我正在查看此链接:Changing columns names in Pandas with hierarchical columns
那位作者说它会在 0.15.0 变得更容易,所以我希望有更多最近更新的解决方案
【问题讨论】:
标签: regex python-2.7 pandas wildcard