简体   繁体   English

如果包含NaN的列如何转换为int?

[英]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 ,我想将RankQuartile列中的所有值都转换为整数。 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.

相关问题 如何转换还包含整数 NaN 的年份行? - How can I convert a row of years that also contains NaNs in integers? 在 Pandas 中,如何仅在包含 NaN 的列中对数值执行 .diff() 方法? - In Pandas, how can I perform the .diff() method to numerical values only in a column that also contains NaNs? 将包含 NaN 的 Pandas 列转换为 dtype `int` - Convert Pandas column containing NaNs to dtype `int` 如何将 Int64 列转换为浮点数 - How can I convert Int64 column in float 在NaD中将字符串转换为pandas中的int - Convert string with NaNs to int in pandas 我如何将文本文件的第一行转换为python转义NaN中的列表? - how can i convert first row of a text file into list in python escaping NaNs? 将一系列浮点数转换为 int - 列表中的某些 NaN 导致错误“无法将浮点 NaN 转换为整数”。 如何跳过NaN? - Converting a series of floats to int - some NaNs in list are causing an error 'cannot convert float NaN to integer'. How to skip NaNs? 如何让 Pandas 将包含 NaT 的列从 timedelta 转换为 datetime? - How can I make Pandas convert a column which contains NaT from timedelta to datetime? 我可以用分组数据框中的列模式替换Nans吗? - Can I replace Nans with the mode of a column in a grouped data frame? 如果列包含某个值,则替换 dataframe 中的 Nans - Replace Nans in dataframe if a column contains a certain value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM