在学校学习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 DataFrame
Columns: [common_name, scientific_name, status]
Index: [], common_name scientific_name status
0 Addax Addax nasomaculatus Endangered, Empty DataFrame
Columns: [common_name, scientific_name, status]
Index: [], common_name scientific_name status
1 Alligator, Chinese Alligator sinensis Endangered, common_name scientific_name status
1 Anoa, lowland Bubalus depressicornis Endangered, Empty DataFrame
Columns: [common_name, scientific_name, status]
Index: [], Empty DataFrame
Columns: [common_name, scientific_name, status]
Index: [], Empty DataFrame
Columns: [common_name, scientific_name, status]
Index: [], ,....我对Python的了解还不够多,不知道为什么它会给我这样的输出。据我所知,它从所有1288行返回数据,并为与列表不匹配的所有行返回“Empty DataFrame Columns: common_name,scientific_name,status Index:[]”。
我如何才能阻止这种情况的发生,并只追加一个包含我需要的行的列表呢?(或者更好的做法是只使用我需要的行创建一个新的数据帧。这就是最终目标。)
发布于 2020-10-25 08:54:35
如果您有要排除的行列表,请尝试执行以下操作:
clean_df = df[~df['scientific_name'].isin(excludeRows)]要获取排除行的df,请执行以下操作
clean_df = df[df['scientific_name'].isin(excludeRows)]clean_df是新的df,df是旧的df,excludeRows是要排除的行值。
https://stackoverflow.com/questions/64519343
复制相似问题