Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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 如何从dataframe列中删除拖尾字母_Python_Regex_Csv_Pandas_Dataframe - Fatal编程技术网

Python 如何从dataframe列中删除拖尾字母

Python 如何从dataframe列中删除拖尾字母,python,regex,csv,pandas,dataframe,Python,Regex,Csv,Pandas,Dataframe,我有一个数据帧: ab 10.1 33.3 11.2 44.2s 12.3 11.3s 14.2秒* 15.4s nan 我希望输出为 ab 10.1 33.3 11.2 44.2 12.3 11.3 十四点二零 15.4.0 如何删除这些尾随字母 我试过这个密码 1st approch: bulb_temp_df['A'].str.extract('(\d')).astype(float) 灯泡温度df['B'].str.extrac

我有一个数据帧:

ab
10.1        33.3
11.2 44.2s
12.3 11.3s
14.2秒*
15.4s nan

我希望输出为

ab
10.1        33.3
11.2        44.2
12.3        11.3
十四点二零
15.4.0

如何删除这些尾随字母 我试过这个密码

1st approch: 
bulb_temp_df['A'].str.extract('(\d')).astype(float)
灯泡温度df['B'].str.extract('(\d').aType(float)

第二种方法:

bulb_temp_df['A']=
灯泡温度df['A'].A类型(str)
灯泡温度df['A']=
灯泡温度df['A']图(λx:x.rstrip('aAbBcC'))

这些都不起作用。他们没有从柱子上清除尾矿

您可以先提取,然后将
NaN
s替换为
0
add

解决方案正在处理多个列

cols = ['A','B']

#if mixed values - numeric with strings
bulb_temp_df[cols]=bulb_temp_df[cols].astype(str)

bulb_temp_df[cols]=bulb_temp_df[cols].apply(lambda x:x.str.extract('(\d+\.\d+)',expand=False)
                                                      .astype(float)
                                                      .fillna(0))
      A     B
0  10.1  33.3
1  11.2  44.2
2  12.3  11.3
3  14.2   0.0
4  15.4   0.0

它返回
NaN
,因此函数
fillna(0)
替换它。谢谢你的接受。您也可以向上投票-单击接受标记上方的
0
小三角形。谢谢。很抱歉造成混淆。它正在从数字中删除“s”,但没有字母的数字将变为0。请您纠正一下这个检查答案,使用
bull\u temp\u df[cols]=bull\u temp\u df[cols].astype(str)
如果我在列中有一些负值,那么这些值就不一致了,这些值就消失了。因此,这个代码< > >(\d++\d+),但是它不会考虑正值,在这种情况下该怎么办。我得到了一个负值,但它失败了。我想你可以使用
([-+]?\d*\。\d+\d+)
,或者省略+like
([-]?\d*。\d+\d+)
我仍然没有找到解决方案。我试着这样做
灯泡温度df[cols]=灯泡温度df[cols]。apply(lambda x:x.str.extract('(\d+\.\d+),expand=False.astype(float.fillna(0))