Python 将列转换为字符串
将列转换为串联字符串的最快方法是什么 例如,如果Python 将列转换为字符串,python,pandas,dataframe,Python,Pandas,Dataframe,将列转换为串联字符串的最快方法是什么 例如,如果df['col1']包含以下内容: col1 word1 word2 word3 返回'word1 word2 word3'的理想方式是什么?选项1]使用str.cat In [3761]: df.col1.str.cat(sep=' ') Out[3761]: 'word1 word2 word3' 选项2]使用join In [3763]: ' '.join(df.col1) Out[3763]: 'word1 word2 word3'
df['col1']
包含以下内容:
col1
word1
word2
word3
返回
'word1 word2 word3'
的理想方式是什么?选项1]使用str.cat
In [3761]: df.col1.str.cat(sep=' ')
Out[3761]: 'word1 word2 word3'
选项2]使用join
In [3763]: ' '.join(df.col1)
Out[3763]: 'word1 word2 word3'
相反,使用列表
,在这种情况下,它会更快
In [3794]: ' '.join(df.col1.values.tolist())
Out[3794]: 'word1 word2 word3'
In [3795]: df.col1.values.tolist()
Out[3795]: ['word1', 'word2', 'word3']
计时 中型 大尺寸
比'.join(df.col1.values.tolist())
df.col1.str.cat(sep='')快得多
str.cat
In [3761]: df.col1.str.cat(sep=' ')
Out[3761]: 'word1 word2 word3'
选项2]使用join
In [3763]: ' '.join(df.col1)
Out[3763]: 'word1 word2 word3'
相反,使用列表
,在这种情况下,它会更快
In [3794]: ' '.join(df.col1.values.tolist())
Out[3794]: 'word1 word2 word3'
In [3795]: df.col1.values.tolist()
Out[3795]: ['word1', 'word2', 'word3']
计时 中型 大尺寸
比'.join(df.col1.values.tolist())
df.col1.str.cat(sep='')快得多