Pandas:将所有列从字符串转换为数字,除了两个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44602139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:48:41  来源:igfitidea点击:

Pandas: convert all column from string to number except two?

pythonpandas

提问by Learner132

Suppose we have

假设我们有

>>> df.dtype Name object Height object Weight object Age object Job object

>>> df.dtype Name object Height object Weight object Age object Job object

Is there any simple way to covert all columns except Name and Job columns with .to_numeric() method?

是否有任何简单的方法可以使用 .to_numeric() 方法隐藏除 Name 和 Job 列之外的所有列?

I have tried but it doesn't work

我试过了,但没有用

df.iloc[df.columns != Name & df.columns != Job] = pd.to_numeric(df.iloc[df.columns != Name & df.columns != Job], errors='coerce')

df.iloc[df.columns != Name & df.columns != Job] = pd.to_numeric(df.iloc[df.columns != Name & df.columns != Job], errors='coerce')

回答by baloo

The simplest way that comes to my mind would be to make a list of all the columns except Name and Job and then iterate pandas.to_numericover them:

我想到的最简单的方法是列出除 Name 和 Job 之外的所有列,然后遍历pandas.to_numeric它们:

cols=[i for i in df.columns if i not in ["Name","Job"]]
for col in cols:
    df[col]=pd.to_numeric(df[col])

Edit:

编辑:

If you absolutely want to use numbers instead of columns names and already know at which indice they are:

如果您绝对想使用数字而不是列名并且已经知道它们位于哪个索引:

for i in [i for i in list(range(len(df.columns))) if i not in [0,4]]:
    df.iloc[:,i]=pandas.to_numeric(df.iloc[:,i])

That's more complicated than necessary though.

但这比必要的要复杂。

回答by Allen

Suppose you have DF:

假设你有 DF:

df
Out[125]: 
  Name Height Weight Age Job
0    0      2      3   4   5

df.dtypes
Out[126]: 

Name      object
Height    object
Weight    object
Age       object
Job       object
dtype: object

If you have to use pd.to_numeric to convert those columns, you can do it this way:

如果您必须使用 pd.to_numeric 来转换这些列,您可以这样做:

df2 = pd.concat([pd.DataFrame([pd.to_numeric(df[e],errors='coerce') \
                               for e in df.columns if e not in ['Name','Job']]).T,\
                 df[['Name','Job']]],axis=1)


df2
Out[138]: 
   Height  Weight  Age Name Job
0       2       3    4    0   5

df2.dtypes
Out[139]: 
Height     int64
Weight     int64
Age        int64
Name      object
Job       object
dtype: object