Search code examples
python-3.xpandasdataframeconcatenationunary-operator

Pandas, concatenating values of columns.


I have found answers to this question on here before, but none of them seem to work for me. Right now I have a data frame with a list of clients and their address. However, each address is separated into many columns and i'm trying to put them all under one.

The code I have so far read as so:

data1_df['Address'] = data1_df['Address 1'].map(str) + ", " + data1_df['Address 2'].map(str) + ", " +  data1_df['Address 3'].map(str) + ", " + data1_df['city'].map(str) + ", " +  data1_df['city'].map(str) + ", " +  data1_df['Province/State'].map(str) + ", " +  data1_df['Country'].map(str) + ", " +  data1_df['Postal Code'].map(str)  

However, the error I get is: TypeError: Unary plus expects numeric dtype, not object

I'm not sure why it's not accepting the strings as they are and using the + operator. Shouldn't the plus accommodate objects?


Solution

  • Hopefully you'll find this example helpful:

    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame({'A': [1,2,3],
                       'B': list('ABC'),
                       'C': [4,5,np.nan],
                       'D': ['One', np.nan, 'Three']})
    
    addColumns = ['B', 'C', 'D']
    
    df['Address'] = df[addColumns].astype(str).apply(lambda x: ', '.join([i for i in x if i != 'nan']), axis=1)
    
    df
    
    #   A  B    C      D      Address
    #0  1  A  4.0    One  A, 4.0, One
    #1  2  B  5.0    NaN       B, 5.0
    #2  3  C  NaN  Three     C, Three
    

    The above will work as str representation of NaN is nan.

    Or you can make it with filling NaN with empty strings:

    df['Address'] = df[addColumns].fillna('').astype(str).apply(lambda x: ', '.join([i for i in x if i]), axis=1)