2017-06-06 91 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()]]