Home > Enterprise >  Remove rows with more than one hyphen in Pandas
Remove rows with more than one hyphen in Pandas

Time:10-16

I am wondering if it's possible to remove the row with more than one hyphens - like two hyphens but keep the rows with one hyphen.

import pandas as pd
import datetime as dt

df = pd.DataFrame({
    'apple_pie': ["Hong Kong - London - New York", "Fuji Apple - Best apple pie"],
    'shipped_date': ["2021-09-23 21:24:06", "2021-09-25 11:24:06"]
}) 


                       apple_pie         shipped_date
0  Hong Kong - London - New York  2021-09-23 21:24:06
1    Fuji Apple - Best apple pie  2021-09-25 11:24:06

Expected output

                       apple_pie         shipped_date
1    Fuji Apple - Best apple pie  2021-09-25 11:24:06

CodePudding user response:

If need filter one or zero hyphens use boolean indexing with Series.str.count with Series.le:

df[df['apple_pie'].str.count('-').le(1)]

Or is possible also add spaces:

df[df['apple_pie'].str.count(' - ').le(1)]
  • Related