0

I use the following code to filter out some rows in my data frame:

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

But I got the following errors:

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'

Does anyone know what I missed? Thanks!

1 Answer 1

2

You just need to add .str in there to use the Pandas string methods:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.