-1

I have the following dataframe:

index        text        is_retweet
0            Test        False
1            RT bar      False
2            RT bazz     False
3            Test2       False

I want to delete the rows that begin with "RT"

I tried this:

my_df.drop(my_df.index[my_df['text'].find("RT") == 0], inplace = True)

But I get this error:

AttributeError: 'Series' object has no attribute 'find'
Neil
  • 5,838
  • 11
  • 61
  • 144

2 Answers2

1

Use pandas.Series.str.startswith:

new_df = df[~df["text"].str.startswith("RT")]
print(new_df)

Output:

   index   text  is_retweet
0      0   Test       False
3      3  Test2       False
Chris
  • 27,139
  • 3
  • 23
  • 44
1

Another option taking the position of characters:

df1 = df[df['text'].str[0:2] != 'RT']
df1

ouput:

    index   text    is_retweet
0       0   Test    False
3       3   Test2   False
David Erickson
  • 15,734
  • 2
  • 16
  • 32