sokeefe
sokeefe

Reputation: 637

Pandas column to string

What is the fastest way to convert a pandas column to one concatenated string?

For example, if df['col1'] contained the below:

col1
word1
word2
word3

What is the ideal way to return 'word1 word2 word3'?

Upvotes: 0

Views: 159

Answers (1)

Zero
Zero

Reputation: 76917

Option 1] Use str.cat

In [3761]: df.col1.str.cat(sep=' ')
Out[3761]: 'word1 word2 word3'

Option 2] Use join

In [3763]: ' '.join(df.col1)
Out[3763]: 'word1 word2 word3'

Instead use list which is faster in this case.

In [3794]: ' '.join(df.col1.values.tolist())
Out[3794]: 'word1 word2 word3'

In [3795]: df.col1.values.tolist()
Out[3795]: ['word1', 'word2', 'word3']

Timings

Mid-size

In [3769]: df.shape
Out[3769]: (30000, 1)

In [3770]: %timeit df.col1.str.cat(sep=' ')
100 loops, best of 3: 2.71 ms per loop

In [3771]: %timeit ' '.join(df.col1)
1000 loops, best of 3: 796 µs per loop

In [3788]: %timeit ' '.join(df.col1.values.tolist())
1000 loops, best of 3: 492 µs per loop

Large-size

In [3774]: df.shape
Out[3774]: (300000, 1)

In [3775]: %timeit df.col1.str.cat(sep=' ')
10 loops, best of 3: 29.7 ms per loop

In [3776]: %timeit ' '.join(df.col1)
100 loops, best of 3: 9.22 ms per loop

In [3791]: %timeit ' '.join(df.col1.values.tolist())
100 loops, best of 3: 6.69 ms per loop

  • ' '.join(df.col1.values.tolist()) is much faster than df.col1.str.cat(sep=' ')

Upvotes: 3

Related Questions