Jake Wagner
Jake Wagner

Reputation: 826

How to drop these rows in Pandas?

I am trying to drop the rows in my pandas dataframe that have no price in them. Technically, in the data source the dollar sign ($) appears by default so df1.dropna(subset=['Price ($)'], inplace = True) does not work in my case. How can I drop the rows that have no price attached to them.

import pandas as pd

first = pd.read_excel('......')
df1 = pd.DataFrame(first)

   Car                          Price ($)

   Honda                          $200
   Benz                           $             ----------> drop
   Chevy                          $300
   BMW                            $250
   Kia                            $             ----------> drop 

Upvotes: 0

Views: 139

Answers (2)

Vaishali
Vaishali

Reputation: 38415

The other answer would work perfectly fine but here is another approach

df = df[df['Price ($)'].str.contains('\d+')]

This would ensure that you get only the rows where there is a numeric price available in the Price column. As a bonus, this is also faster:)

Upvotes: 1

user7492117
user7492117

Reputation:

If I understand correctly this should work:

first = first[first['Price ($)'] != '$']

This just keeps rows where the Price column isn't equal to '$'

Upvotes: 3

Related Questions