2017-09-22 21 views
1

im使用大熊貓讀取CSV和獲得所有數據類型作爲object刪除尾隨從數據*或#在大熊貓

NO是具有尾隨數值*和#在一些觀察的列。

我試圖

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

如何刪除已在NO列尾隨*或#中的所有行?

回答

1

考慮這個數據幀,

No 
0 1 
1 2# 
2 3 
3 4* 
4 #5 

您可以使用此只刪除尾隨字符,

df['No'] = df['No'].str.replace('[#|*]$', '') 

你得到

 No 
0  1 
1  2 
2  3 
3  4 
4 #5 

更廣義解櫃面要刪除這些字符來自整列並且只保留數字

df['No' ] = df['No'].str.extract('(\d+)', expand = False) 

你得到

No 
0 1 
1 2 
2 3 
3 4 
4 5 
+0

接受的答案值得向上投票( - : – piRSquared

+0

非常感謝你:) :) – Vaishali