簡體   English   中英

刪除 Pandas 中包含多個連字符的行

[英]Remove rows with more than one hyphen in Pandas

我想知道是否可以刪除帶有多個連字符的行 - 就像兩個連字符,但保留帶有一個連字符的行。

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

預期輸出

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

如果需要過濾一個或零個hyphens使用帶有Series.str.countSeries.le boolean indexing

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

或者也可以添加空格:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM