Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python/Pandas-将具有浮点值和Nones的列转换为int值和Nones_Python_Pandas_Lambda - Fatal编程技术网

Python/Pandas-将具有浮点值和Nones的列转换为int值和Nones

Python/Pandas-将具有浮点值和Nones的列转换为int值和Nones,python,pandas,lambda,Python,Pandas,Lambda,我有一列的值是浮点数,我想把它们转换成整数 pdsm: federation_unit_id city_id id 3 8.0 3.0 7 None None 17 8.0 3.0 18 8.0 3.0 19 8.0 9.

我有一列的值是浮点数,我想把它们转换成整数

pdsm:
    federation_unit_id  city_id
id                             
3                  8.0      3.0
7                 None     None
17                 8.0      3.0
18                 8.0      3.0
19                 8.0      9.0
它们的类型是列中的值类型:class'float',但by None是非类型

如果我尝试这样做:

pdsm['federation_unit_id']=pdsm['federation_unit_id'].astype(int)
pdsm['city_id'].iloc[0]=pdsm.city_id.astype(int)
pdsm['federation_unit_id']=pdsm['federation_unit_id'].apply(lambda x: x.astype(int) if x is not None else None)
pdsm['city_id'].iloc[0]=pdsm.city_id.apply(lambda x: x.astype(int) if x is not None else None)
我明白了:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
如果我尝试这样做:

pdsm['federation_unit_id']=pdsm['federation_unit_id'].astype(int)
pdsm['city_id'].iloc[0]=pdsm.city_id.astype(int)
pdsm['federation_unit_id']=pdsm['federation_unit_id'].apply(lambda x: x.astype(int) if x is not None else None)
pdsm['city_id'].iloc[0]=pdsm.city_id.apply(lambda x: x.astype(int) if x is not None else None)
我得到:

AttributeError: 'float' object has no attribute 'astype'

有人能帮忙吗?我要发疯了。

我认为在熊猫中,不能在同一列中包含int、None或nan。目前还不支持它

如果可以将“无”转换为0,则可以执行以下操作:

df.fillna(0).astype(int)
Out[157]: 
    federation_unit_id  city_id
id                             
3                    8        3
7                    0        0
17                   8        3
18                   8        3
19                   8        9

我认为在熊猫中,不能在同一列中包含int和None或nan。目前还不支持它

如果可以将“无”转换为0,则可以执行以下操作:

df.fillna(0).astype(int)
Out[157]: 
    federation_unit_id  city_id
id                             
3                    8        3
7                    0        0
17                   8        3
18                   8        3
19                   8        9

不能将缺少值的列作为int类型。遗憾的是,int数据类型没有缺少值。它一定是浮动的。这将在pandas 2.0中修复。不能将缺少值的列作为int类型。遗憾的是,int数据类型没有缺少值。它一定是浮动的。这将在熊猫2.0中修复