Pandas replace column values with a list

I have a dataframe df where some of the columns are strings and some are numeric. I am trying to convert all of them to numeric. So what I would like to do is something like this:

col = df.ix[:,i]
le = preprocessing.LabelEncoder()
newCol = le.transform(col)
df.ix[:,i] = newCol

but this does not work. Basically my question is how do I delete a column from a data frame then create a new column with the same name as the column I deleted when I do not know the column name, only the column index?

1 answer

  • answered 2017-06-17 19:19 pshep123

    This should do it for you:

    # Find the name of the column by index
    n = df.columns[1]
    # Drop that column
    df.drop(n, axis = 1, inplace = True)
    # Put whatever series you want in its place
    df[n] = newCol

    ...where [1] can be whatever the index is, axis = 1 should not change.

    This answers your question very literally where you asked to drop a column and then add one back in. But the reality is that there is no need to drop the column if you just replace it with newCol.