在學校學習Python。在開發(fā)一個項目時,我想從一個數(shù)據(jù)幀中刪除特定行并將其轉換為另一個數(shù)據(jù)幀。我有一個包含 372 種動物的列表,如果它們的名字出現(xiàn)在數(shù)據(jù)框中(數(shù)據(jù)框有 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 的了解不夠,不知道為什么它會給我這樣的輸出。據(jù)我所知,它返回所有 1288 行的數(shù)據(jù),并為與列表不匹配的所有行返回“空 DataFrame 列:[common_name,science_name,status] 索引:[]”。我怎樣才能阻止這種情況發(fā)生并只附加一個包含我需要的行的列表?(或者更好的是創(chuàng)建一個新的數(shù)據(jù)框,其中僅包含我需要的行。這就是最終目標。)
1 回答

暮色呼如
TA貢獻1853條經(jīng)驗 獲得超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 行值。
添加回答
舉報
0/150
提交
取消