Python将特定的数据帧列转换为integ

2024-06-16 15:39:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个8列的数据帧,我想把最后6列转换成整数。dataframe还包含NaN值,我不想删除它们。在

enter image description here

      a      b    c     d     e     f     g    h
0   john     1   NaN   2.0    2.0  42.0  3.0  NaN
1   david    2  28.0  52.0   15.0  NaN   2.0  NaN
2   kevin    3   1.0   NaN   1.0   10.0  1.0  5.0

有什么想法吗?在

谢谢。在


Tags: 数据dataframe整数nanjohndavidkevin
2条回答

多亏了MaxU,我用nan=-1添加了这个选项:

Reason: nan values are float values and can't coexist with integers. So either nan values and floats or the option to think of -1 as nan

http://pandas.pydata.org/pandas-docs/version/0.20/generated/pandas.to_numeric.html

import pandas as pd
import numpy as np

df = pd.DataFrame.from_dict({'a': {0: 'john', 1: 'david', 2: 'kevin'},
 'b': {0: 1, 1: 2, 2: 3},
 'c': {0: np.nan, 1: 28.0, 2: 1.0},
 'd': {0: 2.0, 1: 52.0, 2: np.nan},
 'e': {0: 2.0, 1: 15.0, 2: 1.0},
 'f': {0: 42.0, 1: np.nan, 2: 10.0},
 'g': {0: 3.0, 1: 2.0, 2: 1.0},
 'h': {0: np.nan, 1: np.nan, 2: 5.0}})

df.iloc[:, -6:] = df.iloc[:, -6:].fillna(-1)
df.iloc[:, -6:] = df.iloc[:, -6:].apply(pd.to_numeric, downcast='integer')

df

    a   b   c   d   e   f   g   h
0   john    1   -1  2   2   42  3   -1
1   david   2   28  52  15  -1  2   -1
2   kevin   3   1   -1  1   10  1   5

谢谢@AntonvBR for the ^{} hint

In [29]: df.iloc[:, -6:] = df.iloc[:, -6:].apply(pd.to_numeric, errors='coerce', downcast='integer')

In [30]: df
Out[30]:
       a  b     c     d   e     f  g    h
0   john  1   NaN   2.0   2  42.0  3  NaN
1  david  2  28.0  52.0  15   NaN  2  NaN
2  kevin  3   1.0   NaN   1  10.0  1  5.0

In [31]: df.dtypes
Out[31]:
a     object
b      int64
c    float64
d    float64
e       int8
f    float64
g       int8
h    float64
dtype: object

相关问题 更多 >