pandas 将浮点数转换为字符串 [英] Pandas convert float in scientific notation to string
问题描述
我用read_csv()
加载了一个看起来像这样的数据集
I used read_csv()
to load a dataset that looks like this
userid
NaN
1.091178e+11
1.137856e+11
我想将用户ID转换为字符串.一种解决方案是将keep_default_na=False
添加到read_csv()
,此SO建议这样做:
I want to convert the user ids to string. One solution is to add keep_default_na=False
to read_csv()
, which is suggested by this SO: Converting long integers to strings in pandas (to avoid scientific notation)
假设我不想使用keep_default_na=False
.有什么方法可以将用户ID列转换为str.
Let's say I don't want to use keep_default_na=False
. Is there any way to convert the user id column to str.
我尝试了df.userid.astype(str)
,然后又得到了1.091178e+11
.我期望扩展形式的结果而不是科学形式.
I tried df.userid.astype(str)
and I got 1.091178e+11
back. I was expecting the result in the expanded form not scientific form.
我该怎么办?
推荐答案
您可以使用评论:
print (df.userid.map(lambda x: '{:.0f}'.format(x)))
0 nan
1 109117800000
2 113785600000
Name: userid, dtype: object
df.userid = df.userid.map(lambda x: '{:.0f}'.format(x))
print (df)
userid
0 nan
1 109117800000
2 113785600000
我想知道map
是否会更快,但这是相同的:
I wondered whether map
would be faster, but it is the same:
#[300000 rows x 1 columns]
df = pd.concat([df]*100000).reset_index(drop=True)
#print (df)
In [40]: %timeit (df.userid.map(lambda x: '{:.0f}'.format(x)))
1 loop, best of 3: 211 ms per loop
In [41]: %timeit (df.userid.apply(lambda x: '{:.0f}'.format(x)))
1 loop, best of 3: 210 ms per loop
另一种解决方案是 to_string
,但速度很慢:
Another solution is to_string
, but it is slow:
print(df.userid.to_string(float_format='{:.0f}'.format))
0 nan
1 109117800000
2 113785600000
In [41]: (df.userid.to_string(float_format='{:.0f}'.format))
1 loop, best of 3: 2.52 s per loop
这篇关于 pandas 将浮点数转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!