Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:32:38+00:00 2026-06-09T20:32:38+00:00

As a follow up to this post , I would like to concatenate a

  • 0

As a follow up to this post, I would like to concatenate a number of columns based on their index but I am encountering some problems. In this example I get an Attribute error related to the map function. Help around this error would be appreciated as would code that does the equivalent concatenation of columns.

    #data
    df = DataFrame({'A':['a','b','c'], 'B':['d','e','f'], 'C':['concat','me','yo'], 'D':['me','too','tambien']})

    #row function to concat rows with index greater than 2
    def cnc(row):
        temp = []
        for x in range(2,(len(row))):
            if row[x] != None:
                temp.append(row[x])
        return map(concat, temp)

    #apply function per row
    new = df.apply(cnc,axis=1)

    #Expected Output
    new

    concat me
    me too
    yo tambien

thanks,
zach cp

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:32:40+00:00Added an answer on June 9, 2026 at 8:32 pm

    How about something like this?

    >>> from pandas import *
    >>> df = DataFrame({'A':['a','b','c'], 'B':['d','e','f'], 'C':['concat','me','yo'], 'D':['me','too','tambien']})
    >>> df
       A  B       C        D
    0  a  d  concat       me
    1  b  e      me      too
    2  c  f      yo  tambien
    >>> df.columns[2:]
    Index([C, D], dtype=object)
    >>> df[df.columns[2:]]
            C        D
    0  concat       me
    1      me      too
    2      yo  tambien
    >>> [' '.join(row) for row in df[df.columns[2:]].values]
    ['concat me', 'me too', 'yo tambien']
    >>> df["new"] = [' '.join(row) for row in df[df.columns[2:]].values]
    >>> df
       A  B       C        D         new
    0  a  d  concat       me   concat me
    1  b  e      me      too      me too
    2  c  f      yo  tambien  yo tambien
    

    If you have None objects floating around, you could handle that too. For example:

    >>> df["C"][1] = None
    >>> df
       A  B       C        D
    0  a  d  concat       me
    1  b  e    None      too
    2  c  f      yo  tambien
    >>> rows = df[df.columns[2:]].values
    

    In near-English:

    >>> new = [' '.join(word for word in row if word is not None) for row in rows]
    >>> new
    ['concat me', 'too', 'yo tambien']
    

    Using filter:

    >>> new = [' '.join(filter(None, row)) for row in rows]
    >>> new
    ['concat me', 'too', 'yo tambien']
    

    etc. You could do it in one line but I think it’s clearer to separate it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

need some enlightment here, and this is my first post here. I would like
I follow this rule but some of my colleagues disagree with it and argue
I am tring to follow this post post and combine it with the ShootThemUp
This is a follow up of this [post] Ada: Adding an exception in a
This post if a follow-up question to mt previous post: Android RESTful Web application
I'm trying to follow this tutorial using StructureMap : http://iridescence.no/post/Constructor-Injection-for-ASPNET-MVC-Action-Filters.aspx What I'm trying to
This is a follow-up question related to my previous post . Below is a
I follow this tutorial online exactly but somehow it's giving me errors. Saying there
This question is a follow-up of the post at Ada file operation: instantiation and
This is a kind of follow-up question to this post, where the coloring of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.