Python 为什么我们不能打印dtype=='object'的列名

Python 为什么我们不能打印dtype=='object'的列名,python,python-3.x,pandas,Python,Python 3.x,Pandas,列车=pd.read\U csv 我想你是在找。试试这个: train[train.dtypes=='object'] IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match 或者,如果您只需要列名: df.loc[:, df.dtypes == 'object'].head() 我想你是在

列车=pd.read\U csv


我想你是在找。试试这个:

train[train.dtypes=='object']
IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match
或者,如果您只需要列名:

df.loc[:, df.dtypes == 'object'].head()

我想你是在找。试试这个:

train[train.dtypes=='object']
IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match
或者,如果您只需要列名:

df.loc[:, df.dtypes == 'object'].head()
您可以使用以下方法:

要选择所有非数字列字符串、日期时间等,请执行以下操作:

train.select_dtypes(['object'])
演示:

您可以使用以下方法:

要选择所有非数字列字符串、日期时间等,请执行以下操作:

train.select_dtypes(['object'])
演示:


我在我的机器上运行了你的脚本。我没有看到错误。我在jupyter笔记本电脑C:\ProgramData\Anaconda3\lib\site packages\ipykernel\u launcher上运行。py:2:UserWarning:Boolean系列键将重新编制索引以匹配数据帧索引-----------------------------------当我使用train.dtypes.loc[train.dtypes='object']时,它会打印出来,但不确定这会如何改变。检查此链接是否存在类似问题:我在我的计算机上运行了您的脚本。我没有看到错误。我在jupyter笔记本电脑C:\ProgramData\Anaconda3\lib\site packages\ipykernel\u launcher上运行。py:2:UserWarning:Boolean系列键将重新编制索引以匹配数据帧索引-----------------------------------当我使用train.dtypes.loc[train.dtypes='object']时,它会打印出来,但不确定这会如何改变。检查此链接是否存在类似问题:但是train[train.dtypes=='object']为什么这可能来自您的Pandas版本?我在0.22.0上,我得到了和你一样的错误。也许@Krishna在另一个版本上?@amitparajapt,train[train.dtypes=='object']-这样你就过滤了行,而不是列。更好地使用Ian提供的解决方案,或者更重要一点:train[train.columns[train.dtypes=='object']]但是train[train.dtypes=='object']为什么这可能来自您的熊猫版本?我在0.22.0上,我得到了和你一样的错误。也许@Krishna在另一个版本上?@amitparajapt,train[train.dtypes=='object']-这样你就过滤了行,而不是列。更好地使用Ian提供的解决方案,或者更重要一点:train[train.columns[train.dtypes=='object']]
In [92]: train.select_dtypes(['object']).head(2)
Out[92]:
          Workclass  Education      Marital.Status       Occupation   Relationship   Race   Sex Native.Country  \
0         State-gov  Bachelors       Never-married     Adm-clerical  Not-in-family  White  Male  United-States
1  Self-emp-not-inc  Bachelors  Married-civ-spouse  Exec-managerial        Husband  White  Male  United-States

  Income.Group
0        <=50K
1        <=50K

In [93]: train.select_dtypes(exclude='number').head(2)
Out[93]:
          Workclass  Education      Marital.Status       Occupation   Relationship   Race   Sex Native.Country  \
0         State-gov  Bachelors       Never-married     Adm-clerical  Not-in-family  White  Male  United-States
1  Self-emp-not-inc  Bachelors  Married-civ-spouse  Exec-managerial        Husband  White  Male  United-States

  Income.Group
0        <=50K
1        <=50K