我有一個數據幀:如何從浮點值數據框中列刪除字母
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
如何刪除這些尾礦字母
我曾嘗試此代碼
第一種方法:
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'))
這些不工作。他們不會從列中刪除拖尾。
第三種方法
bulb_temp_df[cols]=bulb_temp_df[cols].apply(lambda x:x.str.extract('(\d+\.\d+)',expand=False)
.astype(float)
.fillna(0))`
所有這些都不能正常工作。最後一個刪除拖尾s,但它將沒有's'的值轉換爲零或南。
嘗試在使用「rstrip」後剝離數據。例子'x.rstrip('aAbBcC')。strip()' –
認爲這是個問題,所以如果你知道如何使用它,你可以這樣做:'x2 <-gsub(「[^ 0-9。]」 ,「」,as.character(unlist(x))); x2 [x2 ==「」] < - 0; x3 <-matrix(as.numeric(x2),5,2); colnames(x3)< - C( 「A」, 「B」); x3'。 – CCurtis