Skip to main content
 首页 » 编程设计

python之如何在标签编码时更改数据框中列的数据类型

2024年11月01日14sxdcgaq8080

我有一个数据框,它的列中有字符和数字。尺寸为 690x12。 数据框看起来像这样:

  A1   A2     A3   A4   A5  ..... 
  b    12.33  c    110  +   ...... 
  a    3.52   q    65   -   ......   
  a    7.44   p    98   +   ...... 
  a    5.01   q    54   - 
  b    10.87  p    33   - 

我的任务是对所有包含字符的列进行标签编码,并返回新的数据帧。

直到现在我尝试过这样的事情:

dat = dataC 
 
for column in dat: 
    col = dat[column] 
    temp = pd.to_numeric(col, errors = 'coerce') 
 
    if(temp.isna().sum() == col.size): 
        col1 = LabelEncoder().fit_transform(col) 
        col1 = pd.DataFrame(col1).astype('int64') 
        dat[column] = np.where(1, col1, dat[column]) 
 
dat.dtypes 

输出是完美的,看起来像:

  A1   A2     A3   A4   A5  ..... 
  1    12.33  0    110  0   ...... 
  0    3.52   2    65   1   ......   
  0    7.44   1    98   0   ...... 
  0    5.01   2    54   1 
  1    10.87  1    33   1 

但是当我打印 dat 的数据类型时:

 object 
 float64 
 object 
 int64 
 object  

我希望标签编码数据为 int64 而不是对象,但我的代码似乎不起作用。我该怎么做?

TIA

请您参考如下方法:

<强> 1。您可以使用 astype('int64') 使用函数检查 列:

def ObjectToInt64(df): 
    for i in df.columns: 
        if isinstance(df.loc[df.index[0],i],int): 
            df[i]=df[i].astype('int64') 
 
ObjectToInt64(dat) 
dat.info() 

Note: check the type of the object type columns, if the type of these elements is different from int then replace int ( in isistance()) with the corresponding type. In my example you can see how to verify it.

2.示例:

s1 = pd.Series([3,4],dtype='object') 
s2 = pd.Series([5,4],dtype='int32') 
s3=  pd.Series([1,4],dtype='int64') 
df=pd.concat([s1,s2,s3],axis=1) 

类型输出:

df.info() 

<class 'pandas.core.frame.DataFrame'> 
RangeIndex: 2 entries, 0 to 1 
Data columns (total 3 columns): 
0    2 non-null object 
1    2 non-null int32 
2    2 non-null int64 
dtypes: int32(1), int64(1), object(1) 
memory usage: 120.0+ bytes 

现在使用:

def ObjectToInt64(df): 
    for i in df.columns: 
        if isinstance(df.loc[df.index[0],i],int): 
            df[i]=df[i].astype('int64') 
 
ObjectToInt64(df) 
df.info() 

类型输出:

df.info() 

<class 'pandas.core.frame.DataFrame'> 
RangeIndex: 2 entries, 0 to 1 
Data columns (total 3 columns): 
0    2 non-null int64 
1    2 non-null int32 
2    2 non-null int64 
dtypes: int32(1), int64(2) 
memory usage: 120.0 bytes 

3 为什么这行得通?

type(df[0][0]) 

输出:

int 

type(df[1][0]) 

输出:

numpy.int32