我想删除列名中的字符串和第一个下划线。
我的尝试:
import re
import pandas as p
pathways[pathways.columns.str.replace(r"^[^KEGG_]", "", "regex=True")]
pathways.columns
追溯:
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:2: FutureWarning:
The default value of regex will change from True to False in a future version.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-190-953d2f19fd5e> in <module>()
1 # Remove the "KEGG_" string from pathways.index
----> 2 pathways[pathways.columns.str.replace(r"^[^KEGG_]", "", "regex=True")]
3 pathways.columns
2 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/strings/object_array.py in _str_replace(self, pat, repl, n, case, flags, regex)
156 pat = re.compile(pat, flags=flags)
157
--> 158 n = n if n >= 0 else 0
159 f = lambda x: pat.sub(repl=repl, string=x, count=n)
160 else:
TypeError: '>=' not supported between instances of 'str' and 'int'
KEGG_1 | KEGG_1_2 | KEGG_1_2_3 | |
---|---|---|---|
First | row | row | row |
Second | row | row | row |
输出:
1 | 1_2 | _2_3 | |
---|---|---|---|
First | row | row | row |
Second | row | row | row |
回答1
您可以使用
pathways.columns = pathways.columns.str.replace(r"^KEGG_", "", regex=True)
你得到 " FutureWarning: The default value of regex will change from True to False in a future version.
" 因为你引用了 regex=True
参数。
您需要将字符串开头的 KEGG_
模式与 ^KEGG_
模式匹配。