Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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 如何在df上使用此工作正则表达式(re)删除冗余的非数字字符星号(*)?_Python_Regex_Pandas - Fatal编程技术网

Python 如何在df上使用此工作正则表达式(re)删除冗余的非数字字符星号(*)?

Python 如何在df上使用此工作正则表达式(re)删除冗余的非数字字符星号(*)?,python,regex,pandas,Python,Regex,Pandas,通过使用下面的代码,我可以使用re将如下字符串:*12.2更改为如下浮点数:12.2: import re numeric_const_pattern = '[-+]? (?: (?: \d* \. \d+ ) | (?: \d+ \.? ) )(?: [Ee] [+-]? \d+ ) ?' rx = re.compile(numeric_const_pattern, re.VERBOSE) print('converted string to float number is', float(r

通过使用下面的代码,我可以使用re将如下字符串:
*12.2
更改为如下浮点数:
12.2

import re
numeric_const_pattern = '[-+]? (?: (?: \d* \. \d+ ) | (?: \d+ \.? ) )(?: [Ee] [+-]? \d+ ) ?'
rx = re.compile(numeric_const_pattern, re.VERBOSE)
print('converted string to float number is', float(rx.findall("*12.2")[0]))

converted string to float number is 12.2
但我有一个大熊猫df,它是:

df = pd.DataFrame([[10, '*41', '-0.01', '2'],['*10.5', 54, 34.2, '*-0.076'], 
                        [65, -32.01, '*344.32', 0.01], ['*32', '*0', 5, 43]])


       0         1         2          3
0      10       *41      -0.01        2
1     *10.5      54       34.2      *-0.076
2      65       -32.01   *344.32      0.01
3     *32       *0        5           43
如何将上述函数应用于此df,以删除所有星号字符,并生成一个完整的浮点数据类型,如下图所示

       0       1       2          3
0      10      41     -0.01       2
1      10.5    54      34.2      -0.076
2      65     -32.01   344.32     0.01
3      32      0       5          43
易于理解的
稍微强壮一点 易于理解的
稍微强壮一点
有点冗长,但这里有一个可行的基于非正则表达式的解决方案,使用
melt
str.rpartition

v = df.melt()['value'].astype(str).str.rpartition('*')[2]
df = pd.DataFrame(v.values.astype(float).reshape(df.shape))

df
       0       1       2     3
0  10.00  10.500   65.00  32.0
1  41.00  54.000  -32.01   0.0
2  -0.01  34.200  344.32   5.0
3   2.00  -0.076    0.01  43.0

有点冗长,但这里有一个可行的基于非正则表达式的解决方案,使用
melt
str.rpartition

v = df.melt()['value'].astype(str).str.rpartition('*')[2]
df = pd.DataFrame(v.values.astype(float).reshape(df.shape))

df
       0       1       2     3
0  10.00  10.500   65.00  32.0
1  41.00  54.000  -32.01   0.0
2  -0.01  34.200  344.32   5.0
3   2.00  -0.076    0.01  43.0

非常感谢你。那么,这个df.replace方法接受regex就像接受re-library一样?非常有趣。是的,它实际上使用了
re
库。但是您必须设置标志
regex=True
非常感谢。那么,这个df.replace方法接受regex就像接受re-library一样?非常有趣。是的,它实际上使用了
re
库。但是您必须设置标志
regex=True
@hyTuev您的某个地方有多个星号吗?@hyTuev您的某个地方有多个星号吗?
v = df.melt()['value'].astype(str).str.rpartition('*')[2]
df = pd.DataFrame(v.values.astype(float).reshape(df.shape))

df
       0       1       2     3
0  10.00  10.500   65.00  32.0
1  41.00  54.000  -32.01   0.0
2  -0.01  34.200  344.32   5.0
3   2.00  -0.076    0.01  43.0