简体   繁体   English

如何从 DataFrame 中删除多个索引?

[英]How can I drop multiple indexes from a DataFrame?

fm2_age = fm1_age.drop(fm1_age.index[(fm1_age > df['age'].median() + 35)])

fm2_age = fm2_age.drop(fm2_age.index[(fm2_age < df['age'].median() - 15)])

How can I simplify these lines onto a single one, I tried using this code but it doesn't seem to work如何将这些行简化为一条,我尝试使用此代码,但似乎不起作用

fm2_age = fm1_age.drop(fm1_age.index[(fm1_age > df['age'].median() + 35) &(fm2_age < df['age'].median() - 15)])

Instead of dropping, you can filter for what you want to keep:您可以过滤您想要保留的内容,而不是丢弃:

keep = df['age'].median() + [-15, 35]
fm1_age[fm11_age.between(*keep)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将这些数据框索引转换为列? - How can I convert these dataframe indexes into a column? 如何按列表删除 dataframe 中的多行? - How can I drop multiple rows in my dataframe by list? 如何使用变量的多个索引扩展/追加列表? - How can I extend/append a list with multiple indexes from a variable? 使用 Pandas,如何从一个系列中的多个索引匹配,匹配到 DataFrame 并替换多个列 - Using Pandas, how can you match from multiple indexes in a series, match to a DataFrame and replace multiple columns 如何从 dataframe 中删除具有某些值的行? - How can I drop rows with certain values from a dataframe? 如何从我的数据框中删除几行? - How can I drop several rows from my Dataframe? 如何在字典中使用包含特定索引和列的列表的字典来创建Pandas DataFrame? - How can I create Pandas DataFrame from a dict with lists with specific indexes and columns in Python3? 如何合并来自同一行和列索引/值的两个熊猫数据框的值? - How can I merge the values from two pandas dataframe which as same row and column indexes/value? 如何在不使用 .drop 命令中的所有级别的情况下从多索引 Dataframe 中删除数据? - How can I drop data from multiindexed Dataframe without using all levels in .drop command? Python Pandas - Dataframe可以有多个索引吗? - Python Pandas - Can Dataframe have multiple indexes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM