Pandas 将字符串列和 NaN(浮点数)转换为整数,保持 NaN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40464033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:22:55  来源:igfitidea点击:

Pandas converting column of strings and NaN (floats) to integers, keeping the NaN

pythonpandastype-conversionnan

提问by mik.ferrucci

I have problems in converting a column which contains both numbers of 2 digits in string format (type: str) and NaN (type: float64). I want to obtain a new column made this way: NaN where there was NaN and integer numbers where there was a number of 2 digits in string format. As an example: I want to obtain column Yearbirth2 from column YearBirth1 like this:

我在转换包含字符串格式(类型:str)和 NaN(类型:float64)的 2 位数的列时遇到问题。我想以这种方式获得一个新列:NaN,其中有 NaN 和整数,其中有字符串格式的 2 位数字。例如:我想从 YearBirth1 列中获取 Yearbirth2 列,如下所示:

YearBirth1  #numbers here are formatted as strings: type(YearBirth1[0])=str
        34  # and NaN are floats: type(YearBirth1[2])=float64.
        76
       Nan
        09
       Nan
        91

YearBirth2  #numbers here are formatted as integers: type(YearBirth2[0])=int
        34  #NaN can remain floats as they were. 
        76
       Nan
         9
       Nan
        91

I have tried this:

我试过这个:

csv['YearBirth2'] = (csv['YearBirth1']).astype(int)

And as I expected i got this error:

正如我所料,我收到了这个错误:

ValueError: cannot convert float NaN to integer

So I tried this:

所以我试过这个:

csv['YearBirth2'] = (csv['YearBirth1']!=NaN).astype(int)

And got this error:

并得到这个错误:

NameError: name 'NaN' is not defined

Finally I have tried this:

最后我试过这个:

csv['YearBirth2'] = (csv['YearBirth1']!='NaN').astype(int)

NO error, but when I checked the column YearBirth2, this was the result:

没有错误,但是当我检查 YearBirth2 列时,结果如下:

YearBirth2:
         1
         1
         1
         1
         1
         1

Very bad.. I think the idea is right but there is a problem to make Python able to understand what I mean for NaN.. Or maybe the method I tried is wrong..

非常糟糕.. 我认为这个想法是正确的,但有一个问题让 Python 能够理解我对 NaN 的意思.. 或者我尝试的方法可能是错误的..

I also used pd.to_numeric() method, but this way i obtain floats, not integers..

我还使用了 pd.to_numeric() 方法,但这样我获得了浮点数,而不是整数..

Any help?! Thanks to everyone!

有什么帮助吗?!谢谢大家!

P.S: csv is the name of my DataFrame; Sorry if I am not so clear, I am on improving with English language!

PS:csv是我的DataFrame的名字;对不起,如果我不太清楚,我正在提高英语水平!

回答by jezrael

You can use to_numeric, but is impossible get intwith NaNvalues - they are always converted to float: see na type promotions.

您可以使用to_numeric,但无法int使用NaN值获取- 它们始终转换为float请参阅 na 类型促销

df['YearBirth2'] = pd.to_numeric(df.YearBirth1, errors='coerce')
print (df)
  YearBirth1  YearBirth2
0         34        34.0
1         76        76.0
2        Nan         NaN
3         09         9.0
4        Nan         NaN
5         91        91.0