Combine two columns of text in pandas dataframe

后端 未结 18 1413
-上瘾入骨i
-上瘾入骨i 2020-11-22 01:32

I have a 20 x 4000 dataframe in Python using pandas. Two of these columns are named Year and quarter. I\'d like to create a variable called p

18条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 01:53

    As many have mentioned previously, you must convert each column to string and then use the plus operator to combine two string columns. You can get a large performance improvement by using NumPy.

    %timeit df['Year'].values.astype(str) + df.quarter
    71.1 ms ± 3.76 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
    
    %timeit df['Year'].astype(str) + df['quarter']
    565 ms ± 22.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
    

提交回复
热议问题