Combine two columns of text in pandas dataframe

后端 未结 18 1123
-上瘾入骨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:43

    Although the @silvado answer is good if you change df.map(str) to df.astype(str) it will be faster:

    import pandas as pd
    df = pd.DataFrame({'Year': ['2014', '2015'], 'quarter': ['q1', 'q2']})
    
    In [131]: %timeit df["Year"].map(str)
    10000 loops, best of 3: 132 us per loop
    
    In [132]: %timeit df["Year"].astype(str)
    10000 loops, best of 3: 82.2 us per loop
    

提交回复
热议问题