2016-12-12 107 views
0

我用下面的代碼在我的數據幀過濾掉一些行的字符串內容:大熊貓數據框:過濾的行基於列

my_df_1 = my_df[my_df.col1.startswith('good_')] 

,但我得到了以下錯誤:

AttributeErrorTraceback (most recent call last) 
<ipython-input-3-dbd2d6731148> in <module>() 
----> 1 my_df_1 = my_df[my_df.col1.startswith('good_')] 

/usr/local/lib/python2.7/dist-packages/pandas/core/generic.pyc in __getattr__(self, name) 
    2742    if name in self._info_axis: 
    2743     return self[name] 
-> 2744    return object.__getattribute__(self, name) 
    2745 
    2746  def __setattr__(self, name, value): 

AttributeError: 'Series' object has no attribute 'starts with' 

有誰知道我錯過了什麼?謝謝!

回答

2

你只需要在裏面添加.str使用熊貓字符串方法:

In [12]: df = pd.DataFrame({'s': ['good_1', 'bad_1', 'good_2']}) 

In [13]: df 
Out[13]: 
     s 
0 good_1 
1 bad_1 
2 good_2 

In [14]: df['s'].str.startswith("good_") 
Out[14]: 
0  True 
1 False 
2  True 
Name: s, dtype: bool 

In [15]: df[df['s'].str.startswith("good_")] 
Out[15]: 
     s 
0 good_1 
2 good_2