簡體   English   中英

使用條件刪除 Python DataFrame 行中的行

[英]Removing Rows in Python DataFrame rows using conditional

我正在嘗試刪除從文件導入並連接我的數據框列表后不需要的數據行。 這是我當前的 DataFrame 的樣子:

                            Best Movie
0                        Movie: Orphan
1                                   2.
2                        Movie: Avatar
3                                   3.
4          Movie: Inglourious Basterds
...                                ...
2371  Movie: The Deep End of the Ocean
2372                               49.
2373         Movie: Drop Dead Gorgeous
2374                               50.
2375                         Movie: Go

我需要刪除所有僅包含數字的行,因此結果如下所示:

                            Best Movie
0                        Movie: Orphan
2                        Movie: Avatar
4          Movie: Inglourious Basterds
...                                ...
2371  Movie: The Deep End of the Ocean
2373         Movie: Drop Dead Gorgeous
2375                         Movie: Go

謝謝您的幫助

一種使用str.match的解決方案

mask = ~df["Best Movie"].str.match(r"^\s*\d+\.$")
res = df[mask]
print(res)

Output

                         Best Movie
0                     Movie: Orphan
2                     Movie: Avatar
4       Movie: Inglourious Basterds
5  Movie: The Deep End of the Ocean
7         Movie: Drop Dead Gorgeous
9                         Movie: Go

更新

要替換“電影:”並重置索引,請執行以下操作:

res = df[mask].reset_index()
res = res["Best Movie"].str.replace(r"^\s*Movie:", "", regex=True)
print(res)

Output

0                        Orphan
1                        Avatar
2          Inglourious Basterds
3     The Deep End of the Ocean
4            Drop Dead Gorgeous
5                            Go
Name: Best Movie, dtype: object

你可以做:

df.loc[~df['Best Movie'].str.match('^\d+.$')]

樣本輸入

df = pd.DataFrame({
    
    "Best_Movie": ["Movie: Orphan", "2.", "Movie: Avatar", "3."]
})

應用 pd.to_numeric。 只有數字的行將被轉換為浮點數,其他行將被標記為 NaN。

df["nums"] = pd.to_numeric(df['Best_Movie'], errors='coerce')

提取具有文本的行(即標記為 nan 的行)

df.loc[df.nums.isnull(), "Best_Movie"]

樣品 output

0    Movie: Orphan
2    Movie: Avatar
Name: Best_Movie, dtype: object

試試下面的。 '|' 基本上是手段或在這種情況下

df[~df['Best Movie'].str.contains('|'.join(str(i) for i in range(10)))] 

暫無
暫無

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

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