2017-06-06 90 views
1

我有一個DF與大量的行:過濾器DF

13790226       0.320  0.001976     
9895d5dis 182.600  0.040450      
105066007     18.890  0.006432      
109067019     52.500  0.034011      
111845014     16.400  0.023974      
11668574e      7.180  0.070714      
113307021      4.110  0.017514      
113679I37      8.180  0.010837      

我想,以獲得行過濾此DF其中指數最後一個字符是不是數字

所需的df:

9895d5dis 182.600 0.040450 
11668574e  7.180 0.070714 

我該怎麼辦?

回答

2
df['is_digit'] = [i[-1].isdigit() for i in df.index.values] 
df[df['is_digit'] == False] 

但我喜歡的正則表達式更好:

df[df.index.str.contains('[A-z]$')] 
2

這裏有一個簡潔的方式,而無需創建一個新的臨時列:

df 
       b   c 
a       
9895d5dis 182.60 0.040450 
105066007 18.89 0.006432 
109067019 52.50 0.034011 
111845014 16.40 0.023974 
11668574e 7.18 0.070714 
113307021 4.11 0.017514 
113679I37 8.18 0.010837 

df[~df.index.str[-1].str.isnumeric()] 
       b   c 
a       
9895d5dis 182.60 0.040450 
11668574e 7.18 0.070714 
2

是要篩選索引或柱的柱?如果其列

df1 = df[df[0].str.contains('[A-Za-z]')] 

返回

0   1  2 
1 9895d5dis 182.60 0.040450 
5 11668574e 7.18 0.070714 
7 113679I37 8.18 0.010837 #looks like read_clipboard is reading 1 in 113679137 as I 

如果它的索引,首先做

df = df.reset_index() 
+0

大使用你自己的名字! – piRSquared

+0

@piRSquared,這是我對正則表達式的愛:) – Vaishali

+1

我在之前的答案中使用過「A-Za-z''純粹是爲了你的好處:-) – piRSquared

0

扔進組合這樣的:

df.loc[[x for x in df.index if x[-1].isalpha()]]