【发布时间】:2015-02-24 18:27:41
【问题描述】:
关注this recipe。我试图通过包含字符串“+”的列名过滤数据框。示例如下:
B = pd.DataFrame([[1, 5, 2], [2, 4, 4], [3, 3, 1], [4, 2, 2], [5, 1, 4]],
columns=['A', '+B', '+C'], index=[1, 2, 3, 4, 5])
所以我想要一个数据框 C,其中只有“+B”和“+C”列。
C = B.filter(regex='+')
但是我得到了错误:
File "c:\users\hernan\anaconda\lib\site-packages\pandas\core\generic.py", line 1888, in filter
matcher = re.compile(regex)
File "c:\users\hernan\anaconda\lib\re.py", line 190, in compile
return _compile(pattern, flags)
File "c:\users\hernan\anaconda\lib\re.py", line 244, in _compile
raise error, v # invalid expression
error: nothing to repeat
配方说它是 Python 3。我使用 python 2.7。不过,我认为这不是问题所在。
埃尔南
【问题讨论】:
标签: python-2.7 pandas filter dataframe