I'm trying to use a select statement to drop all rows from a dateframe where the values in a certain column don't begin with 126.1.
An example of my data set is:
File Date Time RA Dec
ad0147.fits 18-02-13 22:26:01.779 126.109510 27.360011
ad0147.fits 18-02-13 22:26:01.779 126.061077 27.361124
ad0147.fits 18-02-13 22:26:01.779 125.994430 27.363504
I want to filter out all RA values that do not begin with 126.1.
I used this:
data2 = data2.drop(data2[str(data2['RA'])[0:5] is not str(126.1)].index)
where data2 is my dataframe.
It is returning the error "KeyError: True".
How can I fix this?
str(data2['RA'])[0:5] is not str(126.1)is doing? You should try breaking it up into individual parts and seeing what it's doing.isandis notto compare strings, use==and!=.str(data2['RA'])[0:5]in your case evaluates to'0 '. That converts the slice of the series to a string, which will include the index. It's good practice to break up complicated logic like yours into smaller, more manageable pieces.