亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 for 語句從數據框中附加列表

使用 for 語句從數據框中附加列表

弒天下 2023-12-29 10:02:00
在學校學習Python。在開發一個項目時,我想從一個數據幀中刪除特定行并將其轉換為另一個數據幀。我有一個包含 372 種動物的列表,如果它們的名字出現在數據框中(數據框有 1288 行,每行都是不同的動物),我想刪除該行。因此,我找到了刪除行的解決方案:ess_aza = []for i in aza_names:    if True:        ess_aza.append(ess_clean.loc[ess_clean['scientific_name'] == i])    else:        return列表打印出來后的樣子是這樣的:[Empty DataFrameColumns: [common_name, scientific_name, status]Index: [],   common_name      scientific_name      status0       Addax  Addax nasomaculatus  Endangered, Empty DataFrameColumns: [common_name, scientific_name, status]Index: [],           common_name     scientific_name      status1  Alligator, Chinese  Alligator sinensis  Endangered,      common_name         scientific_name      status1  Anoa, lowland  Bubalus depressicornis  Endangered, Empty DataFrameColumns: [common_name, scientific_name, status]Index: [], Empty DataFrameColumns: [common_name, scientific_name, status]Index: [], Empty DataFrameColumns: [common_name, scientific_name, status]Index: [], ,....我對 Python 的了解不夠,不知道為什么它會給我這樣的輸出。據我所知,它返回所有 1288 行的數據,并為與列表不匹配的所有行返回“空 DataFrame 列:[common_name,science_name,status] 索引:[]”。我怎樣才能阻止這種情況發生并只附加一個包含我需要的行的列表?(或者更好的是創建一個新的數據框,其中僅包含我需要的行。這就是最終目標。)
查看完整描述

1 回答

?
暮色呼如

TA貢獻1853條經驗 獲得超9個贊

如果您有要排除的行列表,請嘗試以下操作:

clean_df = df[~df['scientific_name'].isin(excludeRows)]

獲取排除行的 df

clean_df = df[df['scientific_name'].isin(excludeRows)]

clean_df 是新的 df,df 是舊的 df,excludeRows 是要排除的第 3 行值。


查看完整回答
反對 回復 2023-12-29
  • 1 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號