I'm stuck with a pandas series that's containing document numbers but have been imported as float64 values. Some are missing.
我坚持使用一个包含文档编号但已导入为float64值的pandas系列。有些人不见了。
Converting the series to string adds a ".0" to each number or changes the number to e-notation.
将系列转换为字符串会为每个数字添加“.0”或将数字更改为电子表示法。
Converting to integer causes an error message: ValueError: Cannot convert NA to integer
转换为整数会导致错误消息:ValueError:无法将NA转换为整数
Example:
s = pd.Series([129944444999999922.0, 1001.0, 1119999999912.0, None])
s.astype('str')
prints
0 1.29944445e+17
1 1001.0
2 1.11999999991e+12
3 nan
dtype: object
How can I convert the series to show the document number as just the number, no e+ notation and the nan value as an empty string?
如何转换系列以将文档编号显示为数字,没有e +表示法,将nan值显示为空字符串?
1 个解决方案
#1
3
Use 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, '']
#1
3
Use 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, '']