我有幾個(gè)不同的數(shù)據(jù)框,我需要從中刪除某些行。每個(gè)數(shù)據(jù)框都有相同的行序列但位于不同的區(qū)域 Summary Results Report Unnamed: 0 Unnamed: 1 Unnamed: 2 Unnamed: 30 DEM President NaN NaN NaN NaN1 Vote For 1 NaN NaN NaN NaN2 NaN NaN Ballots By NaN Election3 TOTAL NaN NaN Early Voting NaN4 NaN NaN Mail NaN Day5 Tom Steyer NaN 0 0 0 06 Andrew Yang NaN 0 0 0 07 John K. Delaney NaN 0 0 0 08 Cory Booker NaN 0 0 0 09 Michael R. Bloomberg NaN 4 1 1 210 Julian Castro NaN 0 0 0 011 Elizabeth Warren NaN 1 0 1 012 Marianne Williamson NaN 0 0 0 013 Deval Patrick NaN 0 0 0 014 Robby Wells NaN 0 0 0 015 Amy Klobuchar NaN 3 1 2 016 Tulsi Gabbard NaN 0 0 0 0我要?jiǎng)h除的是第一列中包含的行Vote for 1,以及以下 3 行。問(wèn)題是它們可以在多個(gè)區(qū)域顯示,有時(shí)會(huì)顯示多次(例如第二個(gè)數(shù)據(jù)幀)。我所擁有的似乎正在工作,在它刪除所需行的方面,但是,最后,它會(huì)給我一個(gè)key error告訴我它正在重新循環(huán)而沒有任何數(shù)據(jù)的信息。
1 回答

胡說(shuō)叔叔
TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
不確定您的列名是什么,但是摘要列是否包含名稱和您要?jiǎng)h除的幾個(gè)名稱,這應(yīng)該有效。否則您可能必須相應(yīng)地更改列名。
strings_to_remove = ['Vote for 1', 'Total', 'NaN']
df[~df.summary.isin(strings_to_remove)]
添加回答
舉報(bào)
0/150
提交
取消