Python Pandas – 从 DataFrame 列中的字符串中删除数字
要从字符串中删除数字,我们可以使用replace()方法并简单地替换。让我们首先导入 require 库 -
import pandas as pd
使用学生记录创建 DataFrame。Id 列有带数字的字符串 -
dataFrame = pd.DataFrame({
"Id": ['S01','S02','S03','S04','S05','S06','S07'],"Name": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'],"Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass']
}
)
从特定列的字符串中删除数字,即此处的“Id” -
dataFrame['Id'] = dataFrame['Id'].str.replace('\d+', '')
示例
以下是代码 -
import pandas as pd输出结果# 使用学生记录创建 DataFrame
dataFrame = pd.DataFrame(
{
"Id": ['S01','S02','S03','S04','S05','S06','S07'],"Name": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'],"Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass']
}
)
print"DataFrame ...\n",dataFrame
# 从特定列的字符串中删除数字
dataFrame['Id'] = dataFrame['Id'].str.replace('\d+', '')
print"\nUpdated DataFrame...\n", dataFrame
这将产生以下输出 -
DataFrame ...Id Name Result
0 S01 Jack Pass
1 S02 Robin Fail
2 S03 Ted Pass
3 S04 Robin Fail
4 S05 Scarlett Pass
5 S06 Kat Pass
6 S07 Ted Pass
Updated DataFrame...
Id Name Result
0 S Jack Pass
1 S Robin Fail
2 S Ted Pass
3 S Robin Fail
4 S Scarlett Pass
5 S Kat Pass
6 S Ted Pass
以上是 Python Pandas – 从 DataFrame 列中的字符串中删除数字 的全部内容, 来源链接: utcz.com/z/362142.html