通过使用下面的代码,我可以使用re将这样的字符串:更改为这样*12.2的浮点数12.2:import renumeric_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          30      10       *41      -0.01        21     *10.5      54       34.2      *-0.0762      65       -32.01   *344.32      0.013     *32       *0        5           43如何将上面的函数应用于此 df 以删除所有星号字符并制作一个完整的 float dtype pandas df 如下所示?       0       1       2          30      10      41     -0.01       21      10.5    54      34.2      -0.0762      65     -32.01   344.32     0.013      32      0       5          43
                    
                    
                2 回答
 
                    
                    
                            慕村9548890
                            
                                
                            
                        
                        
                                                
                    TA贡献1884条经验 获得超4个赞
有点冗长,但这里有一个可行的基于非正则表达式的解决方案,使用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
添加回答
举报
0/150
	提交
		取消
	