Python 如何从DataFrame列中的名称中删除数字和/或括号
在我的专栏中,我有几个国家的名称,它们的名称中包含我需要删除的数字和/或括号 例如:Python 如何从DataFrame列中的名称中删除数字和/或括号,python,pandas,Python,Pandas,在我的专栏中,我有几个国家的名称,它们的名称中包含我需要删除的数字和/或括号 例如: “玻利维亚(多民族国家)”应为“玻利维亚” “瑞士17”应该是“瑞士” 如果这会影响事情的话,相关列也会被设置为我的索引?试试以下方法: In [121]: df Out[121]: expected Bolivia (Plurinational State of) Bolivia Switzerland17
- “玻利维亚(多民族国家)”应为“玻利维亚”
- “瑞士17”应该是“瑞士”
In [121]: df
Out[121]:
expected
Bolivia (Plurinational State of) Bolivia
Switzerland17 Switzerland
In [122]: df.set_index(df.index.str.replace('\s*\(.*?\)\s*', '').str.replace('\d+',''), inplace=True)
In [123]: df
Out[123]:
expected
Bolivia Bolivia
Switzerland Switzerland
In [124]: df.index == df.expected
Out[124]: array([ True, True], dtype=bool)
In [125]: (df.index == df.expected).all()
Out[125]: True
试试这个:
In [121]: df
Out[121]:
expected
Bolivia (Plurinational State of) Bolivia
Switzerland17 Switzerland
In [122]: df.set_index(df.index.str.replace('\s*\(.*?\)\s*', '').str.replace('\d+',''), inplace=True)
In [123]: df
Out[123]:
expected
Bolivia Bolivia
Switzerland Switzerland
In [124]: df.index == df.expected
Out[124]: array([ True, True], dtype=bool)
In [125]: (df.index == df.expected).all()
Out[125]: True
一种不用调用索引就可以完成的方法
import re
df.apply(lambda x : re.sub('\s*\(.*?\)\s*|\d+', '', x))
一种不用调用索引就可以完成的方法
import re
df.apply(lambda x : re.sub('\s*\(.*?\)\s*|\d+', '', x))
让我们看看你试过什么。那么,代码编写服务不是在向我们展示您的尝试吗。代码编写服务也是如此。虽然这段代码可以解决这个问题,但如何以及为什么解决这个问题会真正有助于提高您的文章质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。虽然此代码可能会解决此问题,但如何以及为什么解决此问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。