You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

pandas.get_dummies()遇columns参数报错,Python2可运行的技术问题

Let's break down your issues step by step and fix them:


1. Root Cause of the Errors

a. get_dummies does not have columns attribute

This error pops up because you tried to access .columns directly on the pd.get_dummies() function instead of its return value. The get_dummies() function outputs a pandas DataFrame, which does have a .columns attribute—you just need to make sure you're calling .columns on the result of the function, not the function itself.

b. TypeError: get_dummies() got an unexpected keyword argument 'columns'

This is a version compatibility problem. The columns parameter for pd.get_dummies() was added in pandas 0.15.0. Your Python 2 environment has a pandas version new enough to support this parameter, but your Python 3 environment is running an older pandas build that doesn't recognize it.


2. Fixes to Resolve the Issues

Option 1: Upgrade pandas in your Python 3 environment (Recommended)

The simplest fix is to upgrade pandas to a version that supports the columns parameter. Run this command in your Python 3 terminal:

pip install --upgrade pandas

Once upgraded, you can simplify your code to avoid looping entirely (it's more efficient):

import pandas as pd
# Convert all categorical columns in one go
fullSet = pd.get_dummies(fullSet, columns=categoricalNames.columns.tolist())

Note: If categoricalNames is just a list of column names (not a DataFrame), remove .columns and pass the list directly: columns=categoricalNames.

Option 2: Compatibility Code for Older pandas Versions

If you can't upgrade pandas for some reason, manually one-hot encode each column and merge the results back into your dataset:

import pandas as pd
for col in categoricalNames.columns:
    # Generate one-hot encoding for the column with a prefix to avoid duplicate names
    dummy_cols = pd.get_dummies(fullSet[col], prefix=col)
    # Add the new dummy columns to your dataset
    fullSet = pd.concat([fullSet, dummy_cols], axis=1)
    # Remove the original categorical column (since we've encoded it)
    fullSet.drop(col, axis=1, inplace=True)

Again, if categoricalNames is a list instead of a DataFrame, loop over categoricalNames directly instead of categoricalNames.columns.


Quick Check to Verify

To confirm your pandas version, run this snippet:

import pandas as pd
print(pd.__version__)

Make sure it's 0.15.0 or higher if you want to use the columns parameter.

内容的提问来源于stack exchange,提问作者goddol

火山引擎 最新活动