-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
  • 4,578
  • 14
  • 70
  • 155

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
  • 29,127
  • 3
  • 28
  • 51
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
  • 16,433
  • 2
  • 19
  • 35