yanachen
yanachen

Reputation: 3753

pandas applying function to columns array is very slow

  os  hour  day
0  13    14    0
1  19    14    0
2  13    14    0
3  13    14    0
4  13    14    0

Here is my dataframe and I just want to get a new column which is str(os)+'_'+str(hour)+'_'str(day). I use apply function to process the dataframe but it is very slow. Any high-performance method to realize this ? I also tried convert the df to array and process every row. It seems that it is slow too. There are nearly two hundred millions rows of the dataframe.

Upvotes: 0

Views: 91

Answers (1)

Vaishali
Vaishali

Reputation: 38415

Not sure what code are you using but you can try

df.astype(str).apply('_'.join, axis = 1)

0    13_14_0
1    19_14_0
2    13_14_0
3    13_14_0
4    13_14_0

Upvotes: 1

Related Questions