【发布时间】:2017-11-25 08:57:48
【问题描述】:
我有一个数据框:
A B
10.1 33.3
11.2 44.2s
12.3 11.3s
14.2s *
15.4s nan
我想输出为
A B
10.1 33.3
11.2 44.2
12.3 11.3
14.2 0
15.4 0
如何删除这些拖尾字母 我试过这段代码
1st approch:
bulb_temp_df['A'].str.extract('(\d)').astype(float)
bulb_temp_df['B'].str.extract('(\d)').astype(float)
第二个方法:
bulb_temp_df['A'] =
bulb_temp_df['A'].astype(str)
bulb_temp_df['A'] =
bulb_temp_df['A'].map(lambda x: x.rstrip('aAbBcC'))
这些都不起作用。他们没有从列中删除 tailing 。
【问题讨论】:
-
我仍然没有找到解决方案尝试这样做
bulb_temp_df[cols]=bulb_temp_df[cols].apply(lambda x:x.str.extract('(\d+\.\d+)',expand=False) .astype(float) .fillna(0))
标签: python regex csv pandas dataframe