![](/img/trans.png)
[英]TypeError: cannot convert the series to <class 'float'> / How do I apply a pandas df to a class object function?
[英]In pandas, how do I convert a series of float or none to strings with integers
我堅持使用包含文檔編號但已導入為float64值的pandas系列。 有些人不見了。
將系列轉換為字符串會為每個數字添加“.0”或將數字更改為電子記數法。
轉換為整數會導致錯誤消息: ValueError:無法將NA轉換為整數
例:
s = pd.Series([129944444999999922.0, 1001.0, 1119999999912.0, None])
s.astype('str')
版畫
0 1.29944445e+17
1 1001.0
2 1.11999999991e+12
3 nan
dtype: object
如何轉換系列以將文檔編號顯示為數字,沒有e +表示法,將nan值顯示為空字符串?
使用list comprehension
:
s1 = pd.Series(['' if pd.isnull(x) else int(x) for x in s], index=s.index)
print (s1.apply(type))
0 <class 'int'>
1 <class 'int'>
2 <class 'int'>
3 <class 'str'>
dtype: object
print (s1.tolist())
[129944444999999920, 1001, 1119999999912, '']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.