简体   繁体   English

将pandas整数系列转换为字符串的最有效方法?

[英]Most efficient way to convert pandas series of integers to strings?

Is .astype(str) the most efficient way to convert a series of ints to a series of strings? .astype(str)是将一系列int转换为一系列字符串的最有效方法吗? It seems rather slow so I wanted to ask. 似乎很慢,所以我想问。

I tried a few things and found a faster way using numpy: 我尝试了几件事,发现使用numpy的更快方法:

setup = """
import pandas, numpy
s = pandas.Series(numpy.random.randint(1,10,(100)))
"""

>>> timeit.timeit('s.astype(str)', setup=setup, number=10000)
3.33058500289917
>>> timeit.timeit('s.apply(str)', setup=setup, number=10000)
3.572000026702881
>>> timeit.timeit('s.apply(lambda x: str(x))', setup=setup, number=10000)
3.821247100830078
>>> timeit.timeit('s.values.astype(numpy.str)', setup=setup, number=10000)
0.08432412147521973

As you can see, accesing the numpy array with values and using the astype call is over 40x faster than the next fastest method. 如您所见,使用values访问numpy数组并使用astype调用比下一个最快的方法快40倍以上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将日期字符串转换为pandas时间序列索引的最有效方法 - Most efficient way to convert date strings to a pandas time series index 将 pandas 系列字符串转换为 numpy 频率矩阵的有效方法 - efficient way to convert pandas series of strings to numpy frequency matrix 将整数字符串列表转换为整数数组的最有效方法 - Most efficient way to turn a list of strings of integers to an array of integers 按日/月/年将熊猫系列日期字符串分类的最有效方法? - Most efficient way to bin a pandas series of date strings by day/month/year? 在pandas中,如何将一系列float或none转换为带整数的字符串 - In pandas, how do I convert a series of float or none to strings with integers 使用Pandas检查2个系列中的值对的最有效方法是? - Most efficient way with Pandas to check pair of values from 2 series? 扩大二进制系列 pandas 的有效区域的最有效方法? - Most efficient way to enlarge the active area of a binary series pandas? 将int64系列转换为日期时间的最有效方法? - Most efficient way to convert an int64 series to datetime? 将空(ish)字符串转换为null的最有效方法 - Most efficient way to convert empty(ish) strings to null 将带有numpy数组列表的字典转换为pandas数据帧的最有效方法? - Most efficient way to convert a dictionary with list of numpy arrays into pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM