0

I'm trying to change Categorical Data from my data frame using the code

CATEGORICAL_COLUMNS = ['sex','n_siblings_spouses', 'parch', 'class',
               'embark_town', 'alone']
for i in CATEGORICAL_COLUMNS:
  dfTrain[i] = pd.factorize(dfTrain.i)[0]
dfTrain.head()

But I get the error:

'DataFrame' object has no attribute 'i'

How would I fix this?

  • 2
    Use `dfTrain[i]` instead? – Michael Butscher Mar 11 '23 at 23:37
  • Does this answer your question? [Accessing Pandas column using squared brackets vs using a dot (like an attribute)](https://stackoverflow.com/questions/41130255/accessing-pandas-column-using-squared-brackets-vs-using-a-dot-like-an-attribute) – Ignatius Reilly Mar 12 '23 at 01:36
  • And similar duplicates [here](https://stackoverflow.com/questions/51343451/for-i-in-list-accessing-and-using-i-to-index-into-a-pandas-dataframe) and [here](https://stackoverflow.com/questions/37883798/accessing-a-pandas-dataframe-column-name-with-a-in-it). The former being exactly your question. – Ignatius Reilly Mar 12 '23 at 01:42

1 Answers1

1

i is not an attribute, you can't use dot notation:

CATEGORICAL_COLUMNS = ['sex','n_siblings_spouses', 'parch', 'class',
                       'embark_town', 'alone']

for i in CATEGORICAL_COLUMNS:
    dfTrain[i] = pd.factorize(dfTrain[i])[0]  # .i -> [i]

Update

If you use sklearn, you can use OrdinalEncoder:

from sklearn.preprocessing import OrdinalEncoder

oe = OrdinalEncoder()
dfTrain[CATEGORICAL_COLUMNS] = oe.fit_transform(dfTrain[CATEGORICAL_COLUMNS])

and use transform_inverse to decode numeric values.

Corralien
  • 109,409
  • 8
  • 28
  • 52