我如何解释这个符号? [英] How do I interpret this notation?
问题描述
符号 s[s] 如何以及为什么起作用?
How and why does the notation s[s] work?
我正在学习 kaggle.com 上的其中一门微课程,它们使用符号 s[s],如下所示.我以前没见过.X_train 是一个 Pandas DataFrame.
I'm taking one of the micro-courses from kaggle.com and they use the notation s[s] as shown below. I have not seen that before. X_train is a pandas DataFrame.
它是一个自我切片的列表吗?有人能帮忙澄清一下吗?
Is it a list slicing itself? Would someone help clarify this?
s = (X_train.dtypes == 'object') ## assigns True to variables == 'object'
object_cols = list(s[s].index)
> s
Type True
Method True
Regionname True
Rooms False
Distance False
Postcode False
Bedroom2 False
Bathroom False
Landsize False
Lattitude False
Longtitude False
Propertycount False
dtype: bool
> s[s]
Type True
Method True
Regionname True
dtype: bool
推荐答案
Pandas DataFrames 允许您索引使用布尔数组 这就是 s
在 []
中的使用方式.如您所见,系列的值是 True
或 False
,因此我们选择 s
的值,其中 s
是 True
.此代码的目的是获取数据类型为 object
的列,您可以使用函数 pandas.DataFrame.select_dtypes
代替:
Pandas DataFrames allow you to index using boolean arrays which is how s
is being used inside []
. The value of the Series is True
or False
as you can see, so we're selecting the values of s
where s
is True
. The purpose of this code is to get columns where the datatype is object
, you can do it with the function pandas.DataFrame.select_dtypes
instead:
list(X_train.select_dtypes(include=['object']).columns)
这篇关于我如何解释这个符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!