Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数据帧是否从字符串中提取int/number值?_Python_Regex_Pandas_Extract - Fatal编程技术网

Python 数据帧是否从字符串中提取int/number值?

Python 数据帧是否从字符串中提取int/number值?,python,regex,pandas,extract,Python,Regex,Pandas,Extract,我有一个列中有值的df。 df: a 0 abc_test_0120_12346 1 def_abc def_0420_9658 a 0 012012346 1 04209658 我只想取出整数/数字。 例外输出为df: a 0 abc_test_0120_12346 1 def_abc def_0420_9658 a 0 012012346 1

我有一个列中有值的df。
df

        a    
0    abc_test_0120_12346
1    def_abc def_0420_9658
        a    
0    012012346
1    04209658
我只想取出整数/数字。
例外输出为
df

        a    
0    abc_test_0120_12346
1    def_abc def_0420_9658
        a    
0    012012346
1    04209658

想法是将非数字替换为emty字符串:

df['a'] = df.a.str.replace('\D+', '')
print (df)
           a
0  012012346
1   04209658

想法是将非数字替换为emty字符串:

df['a'] = df.a.str.replace('\D+', '')
print (df)
           a
0  012012346
1   04209658

我试过这个
df.a.str.extract(“(^\d*)”)
“abc_123 def_456”怎么样?答案是“123456”还是“123456”加空格?我试过这个
df.a.str.extract(“(^\d*)”)
“abc_123 def_456”怎么样?答案是“123456”还是“123456”带有空格?