Combine two columns of text in pandas dataframe

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

    Here is an implementation that I find very versatile:

    In [1]: import pandas as pd 
    
    In [2]: df = pd.DataFrame([[0, 'the', 'quick', 'brown'],
       ...:                    [1, 'fox', 'jumps', 'over'], 
       ...:                    [2, 'the', 'lazy', 'dog']],
       ...:                   columns=['c0', 'c1', 'c2', 'c3'])
    
    In [3]: def str_join(df, sep, *cols):
       ...:     from functools import reduce
       ...:     return reduce(lambda x, y: x.astype(str).str.cat(y.astype(str), sep=sep), 
       ...:                   [df[col] for col in cols])
       ...: 
    
    In [4]: df['cat'] = str_join(df, '-', 'c0', 'c1', 'c2', 'c3')
    
    In [5]: df
    Out[5]: 
       c0   c1     c2     c3                cat
    0   0  the  quick  brown  0-the-quick-brown
    1   1  fox  jumps   over   1-fox-jumps-over
    2   2  the   lazy    dog     2-the-lazy-dog
    

提交回复
热议问题