繁体   English   中英

在 pandas dataframe 中删除列时的一般语法问题

[英]General syntax question when dropping a column within pandas dataframe

season_stats.drop(season_stats [season_stats['Pos'] == 'SF-SG'].index, inplace = True)

在 python 的列中删除特定值时,为什么season_stats在括号内写了两次? 为什么还不够

season_stats.drop(season_stats['Pos'] == 'SF-SG'].index, inplace = True)工作?

这只是一个通用的语法问题数据,不需要提供。

看看你的各种作品的输出。


season_stats['Pos'] == 'SF-SG'

返回一系列真/假值,但pd.DataFrame.drop需要指定索引或列。


(season_stats['Pos'] == 'SF-SG').index

返回您的所有索引,它不能神奇地知道您只想要True值。


因此,我们告诉它我们只想要 True 值,然后取这个过滤后的 DataFrame 的索引:

season_stats[season_stats['Pos'] == 'SF-SG'].index

如果您想花哨,我们可以使用lambda进行过滤,如下所示:

(season_stats['Pos'] == 'SF-SG')[lambda x: x].index

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM