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