[英]How can I convert a column to int if it contains NaNs?
I am working on this efw
data frame: 我正在处理这个efw
数据框:
2 Year ISO_Code Countries INDEX Rank Quartile
3 2016 ALB Albania 7.54 34 1
4 2016 DZA Algeria 4.99 NaN 4
5 2016 AGO Angola 5.17 155 4
6 2016 ARG Argentina 4.84 160 NaN
7 2016 ARM Armenia 7.57 NaN 1
8 2016 AUS Australia 7.98 10 1
9 2016 AUT Austria 7.58 27 NaN
Let's say that I want to convert all the values in the Rank
and Quartile
columns into integers if the are not a NaN
. 假设如果不是NaN
,我想将Rank
和Quartile
列中的所有值都转换为整数。 I have tried efw[["Year", "Rank", "Quartile"]].astype(int)
but keep getting this error: ValueError: cannot convert float NaN to integer
我已经尝试过efw[["Year", "Rank", "Quartile"]].astype(int)
但efw[["Year", "Rank", "Quartile"]].astype(int)
收到此错误: ValueError: cannot convert float NaN to integer
Is there a way to tell Python to "skip" the NaN
values and only convert the others into int
? 有没有办法告诉Python“跳过” NaN
值,而仅将其他值转换为int
?
You can convert them into object
您可以将它们转换为object
efw[["Year", "Rank", "Quartile"]].astype(object)
Out[58]:
Year Rank Quartile
0 2016 34 1
1 2016 NaN 4
2 2016 155 4
3 2016 160 NaN
4 2016 NaN 1
5 2016 10 1
6 2016 27 NaN
#efw[["Year", "Rank", "Quartile"]]=efw[["Year", "Rank", "Quartile"]].astype(object)
I have Just reproduced your requirement, here are few way to do this, However i see you have already an integer values in place and only missing ones places as NaN
which you can convert into int or say zero
as those are missing. 我刚刚重现了您的要求,这是执行此操作的几种方法,但是我看到您已经有一个整数值,并且只有NaN
丢失了这些位置,您可以将其转换为int或说zero
因为这些丢失了。
>>> efw
Year ISO_Code Countries Index Rank Quartile
0 2016 ALB Albania 7.54 34 1
1 2016 DZA Algeria 4.99 NaN 4
2 2016 AGO Angola 5.17 155 4
3 2016 ARG Argentina 4.84 160 NaN
4 2016 ARM Armenia 7.57 NaN 1
5 2016 AUS Australia 7.98 10 1
6 2016 AUT Austria 7.58 27 NaN
Method 1 方法1
Straight from the pandas documentation even we have better way of doing, Document Link For pandas.DataFrame.fillna 即使我们有更好的方法,也可以直接从pandas文档中获取文档链接pandas.DataFrame.fillna
So, As you have multiple columns, but only want to replace the NaN in a subset of them, you can use, I would say better would be to replace NaN
to 0
rather skipping as blank.. 因此,由于您有多个列,但只想替换它们的一个子集中的NaN,可以使用,我会说最好将NaN
替换为0
而不是跳过为空白。
>>> efw.fillna({'Rank':'0', 'Quartile':'0'}, inplace=False)
Year ISO_Code Countries Index Rank Quartile
0 2016 ALB Albania 7.54 34 1
1 2016 DZA Algeria 4.99 0 4
2 2016 AGO Angola 5.17 155 4
3 2016 ARG Argentina 4.84 160 0
4 2016 ARM Armenia 7.57 0 1
5 2016 AUS Australia 7.98 10 1
6 2016 AUT Austria 7.58 27 0
If you want to make the changes to be permanent in the DataFrame then you can use: 如果要将更改永久保留在DataFrame中,则可以使用:
>>> efw.fillna({'Rank':'0', 'Quartile':'0'}, inplace=True)
>>> print(efw)
Method 2 方法二
You could check dtype
whether it's numeric or not with dtype.kind
using apply
for your columns as follows.. 您可以使用dtype.kind
使用apply
列来检查dtype
是否为数字。
*dtype.kind:* A character code (one of 'biufc') identifying the general kind of data. b boolean i signed integer u unsigned integer f floating-point c complex floating-point
Hence, you can use apply with lambda function as follows .. 因此,您可以将apply与lambda函数一起使用,如下所示。
>>> efw.apply(lambda x: x.fillna(0) if x.dtype.kind in 'biufc' else x.fillna('0'))
Year ISO_Code Countries Index Rank Quartile
0 2016 ALB Albania 7.54 34 1
1 2016 DZA Algeria 4.99 0 4
2 2016 AGO Angola 5.17 155 4
3 2016 ARG Argentina 4.84 160 0
4 2016 ARM Armenia 7.57 0 1
5 2016 AUS Australia 7.98 10 1
6 2016 AUT Austria 7.58 27 0
Method 3 方法3
OR With your dataframe you simply use DataFrame.fillna()
method. 或对于您的数据DataFrame.fillna()
您只需使用DataFrame.fillna()
方法。 This will convert all NaN into zero irrespective of Columns you have. 不管您有多少列,这都会将所有NaN都转换为零。
>>> efw.fillna(0)
Year ISO_Code Countries Index Rank Quartile
0 2016 ALB Albania 7.54 34 1
1 2016 DZA Algeria 4.99 0 4
2 2016 AGO Angola 5.17 155 4
3 2016 ARG Argentina 4.84 160 0
4 2016 ARM Armenia 7.57 0 1
5 2016 AUS Australia 7.98 10 1
6 2016 AUT Austria 7.58 27 0
Note: You can use fillna('')
rather fillna(0)
if you want to remove NaN
and make it blank. 注意:如果要删除NaN
并将其留空,则可以使用fillna('')
而不是fillna(0)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.