[英]Pandas replace column values with a list
I have a dataframe df
where some of the columns are strings and some are numeric. 我有一个数据帧df
,其中一些列是字符串,一些是数字。 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()
le.fit(col)
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? 基本上我的问题是如何从数据框中删除一列然后创建一个新列,其名称与我不知道列名时删除的列相同,只列列索引?
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. ...其中[1]
可以是索引, axis = 1
不应该改变。
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
. 这非常简单地回答了你的问题,你要求删除一个列,然后再添加一个。但实际情况是,如果只是用newCol
替换它,就不需要删除列。
newcol = [..,..,.....]
df['colname'] = newcol
This will keep the colname intact while replacing its contents with newcol. 这将保持colname完整,同时用newcol替换其内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.