Combine two columns of text in pandas dataframe

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

    This solution uses an intermediate step compressing two columns of the DataFrame to a single column containing a list of the values. This works not only for strings but for all kind of column-dtypes

    import pandas as pd
    df = pd.DataFrame({'Year': ['2014', '2015'], 'quarter': ['q1', 'q2']})
    df['list']=df[['Year','quarter']].values.tolist()
    df['period']=df['list'].apply(''.join)
    print(df)
    

    Result:

       Year quarter        list  period
    0  2014      q1  [2014, q1]  2014q1
    1  2015      q2  [2015, q2]  2015q2
    
    0 讨论(0)
  • 2020-11-22 01:49

    One can use assign method of DataFrame:

    df= (pd.DataFrame({'Year': ['2014', '2015'], 'quarter': ['q1', 'q2']}).
      assign(period=lambda x: x.Year+x.quarter ))
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 01:52

    Here is my summary of the above solutions to concatenate / combine two columns with int and str value into a new column, using a separator between the values of columns. Three solutions work for this purpose.

    # be cautious about the separator, some symbols may cause "SyntaxError: EOL while scanning string literal".
    # e.g. ";;" as separator would raise the SyntaxError
    
    separator = "&&" 
    
    # pd.Series.str.cat() method does not work to concatenate / combine two columns with int value and str value. This would raise "AttributeError: Can only use .cat accessor with a 'category' dtype"
    
    df["period"] = df["Year"].map(str) + separator + df["quarter"]
    df["period"] = df[['Year','quarter']].apply(lambda x : '{} && {}'.format(x[0],x[1]), axis=1)
    df["period"] = df.apply(lambda x: f'{x["Year"]} && {x["quarter"]}', axis=1)
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-11-22 01:58

    Let us suppose your dataframe is df with columns Year and Quarter.

    import pandas as pd
    df = pd.DataFrame({'Quarter':'q1 q2 q3 q4'.split(), 'Year':'2000'})
    

    Suppose we want to see the dataframe;

    df
    >>>  Quarter    Year
       0    q1      2000
       1    q2      2000
       2    q3      2000
       3    q4      2000
    

    Finally, concatenate the Year and the Quarter as follows.

    df['Period'] = df['Year'] + ' ' + df['Quarter']
    

    You can now print df to see the resulting dataframe.

    df
    >>>  Quarter    Year    Period
        0   q1      2000    2000 q1
        1   q2      2000    2000 q2
        2   q3      2000    2000 q3
        3   q4      2000    2000 q4
    

    If you do not want the space between the year and quarter, simply remove it by doing;

    df['Period'] = df['Year'] + df['Quarter']
    
    0 讨论(0)
提交回复
热议问题