Tae Yamasaki
Tae Yamasaki

Reputation: 25

How to drop a row based on a condition in pandas with string converted to integer for comparison

I am trying to drop all rows where the value is less than 1920 in the Year Built column. I don't understand how to convert to an integer so I can evaluate the less than 1920 year built condition and then remove those buildings built before 1920. Any direction is appreciated!

No error:

YearBuilt_convert_to_integer = dframe[13].astype(int)

No error:

YearBuilt_less_than_1920 = YearBuilt_convert_to_integer < 1920

Output:

YearBuilt_less_than_1920
42      False
43      False
44      False
45       True
46      False
        ...  
3533    False
3534    False
3535    False
3536    False
3537    False
Name: 13, Length: 3347, dtype: bool

Produces 0 rows:

dframe = dframe.loc[~dframe[13].astype(int)<1920, :]

    0   1   2   3   4   5   6   7   8   9   ... 32  33  34  35  36  37  38  39  40  41
0 rows × 42 columns

Upvotes: 0

Views: 132

Answers (2)

Ujjwal Agrawal
Ujjwal Agrawal

Reputation: 998

dframe[np.invert(dframe.loc[:,13].astype(int)<1920)]

Upvotes: 1

swang41
swang41

Reputation: 1

It seems like the pandas getting confuse while you use .loc and Boolean indices at the same time. One simple modification will just use boolean indices dframe_sub = dframe[dframe[13].astype(int) >= 1920].

Upvotes: 0

Related Questions