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

  • Home
  • SEARCH
  • 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 8722095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:19:59+00:00 2026-06-13T07:19:59+00:00

Using Python 3.2 I was hoping to solve the below issue. My data consist

  • 0

Using Python 3.2 I was hoping to solve the below issue. My data consist of hundreds of rows (signifying a project) and 21 columns. The first of which is a unique project ID and the other 20 columns is the group of people, or person, that led the project. person_1 is always filled and if there is a name in person_3 that means 3 people are working together. If there is a name in person_18 that means 18 people are working together.

I have an excel spreadsheet that is setup the following way:

 unique ID person_1      person _2      person_3     person_4   ...  person_20
    12       Tom           Sally          Mike
    16       Joe           Mike
    5        Joe           Sally
    1       Sally          Mike           Tom
    6       Sally          Tom            Mike
    2       Jared          Joe            Mike        John      ...     Carl

I want to do a few things:

1) Make a column that will give me a unique ‘Group Name’ which will be, using unique ID 1 as my example, Sally/Mike/Tom. So it will be the names separated by ‘/’.

2) How can I treat, from my example, Sally/Mike/Tom the same as Sally/Tom/Mike. Meaning, I would like another column that makes the group name in alphabetical order (no matter the actual permutation), still separated by ‘/’.

3) This question is similar to (2). However, I want the person listed in person_1 to matter. Meaning Joe/Tom/Mike is different from Tom/Joe/Mike but not different than Joe/Mike/Tom. So there will be another column that keeps person_1 at the start of the group name but alphabetizes person_2 through person_20 if applicable (i.e., if the project has more than 1 person on it).

Thanks for the help and suggestions

  • 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-13T07:20:00+00:00Added an answer on June 13, 2026 at 7:20 am

    The previous answer gave a clear statement of method, but perhaps you are stuck on either the string processing or the csv processing. Both are demonstrated in the following code. The relevant string methods are sorted and join. '/'.join tells join to use / as separator between joined items. The + operator between lists in tname and writerow statements concatenates the lists. A csv.reader is an iterator that delivers one list per row, and a csv.writer converts a list to a row and writes it out. You will want to add error testing to the file opens, etc. The data file used to test this code is shown after the code.

    import csv
    fi = open('xgroup.csv')
    fo = open('xgroup3.csv', 'w')
    w = csv.writer(fo)
    r = csv.reader(fi)
    li = 0
    print "Opened reader and writer"
    for row in r:
        gname = '/'.join(row[1:])
        sname = '/'.join(sorted(row[1:]))
        tname = '/'.join([row[1]]+sorted(row[2:]))
        w.writerow([row[0], gname, sname, tname]+row[1:])
        li += 1
    fi.close()
    fo.close()
    print "Closed reader and writer after",li,"lines"
    

    File xgroup.csv is shown next.

    unique-ID,person_1,person,_2,person_3,person_4,...,person_20
    12,Tom,Sally,Mike
    16,Joe,Mike
    5,Joe,Sally
    1,Sally,Mike,Tom
    6,Sally,Tom,Mike
    2,Jared,Joe,Mike,John,...,Carl
    

    Upon reading data as above, the program prints Opened reader and writer and Closed reader and writer after 7 lines and produces output in file xgroup3.csv as shown next.

    unique-ID,person_1/person/_2/person_3/person_4/.../person_20,.../_2/person/person_1/person_20/person_3/person_4,person_1/.../_2/person/person_20/person_3/person_4,person_1,person,_2,person_3,person_4,...,person_20
    12,Tom/Sally/Mike,Mike/Sally/Tom,Tom/Mike/Sally,Tom,Sally,Mike
    16,Joe/Mike,Joe/Mike,Joe/Mike,Joe,Mike
    5,Joe/Sally,Joe/Sally,Joe/Sally,Joe,Sally
    1,Sally/Mike/Tom,Mike/Sally/Tom,Sally/Mike/Tom,Sally,Mike,Tom
    6,Sally/Tom/Mike,Mike/Sally/Tom,Sally/Mike/Tom,Sally,Tom,Mike
    2,Jared/Joe/Mike/John/.../Carl,.../Carl/Jared/Joe/John/Mike,Jared/.../Carl/Joe/John/Mike,Jared,Joe,Mike,John,...,Carl
    

    Note, given a data line like

    5,Joe,Sally,,,,,
    

    instead of

    5,Joe,Sally
    

    the program as above produces

    5,Joe/Sally/////,/////Joe/Sally,Joe//////Sally,Joe,Sally,,,,,
    

    instead of

    5,Joe/Sally,Joe/Sally,Joe/Sally,Joe,Sally
    

    If that’s a problem, filter out empty entries. For example, if
    row=['5', 'Joe', 'Sally', '', '', '', '', ''], then
    '/'.join(row[1:]) produces
    'Joe/Sally/////', while
    '/'.join(filter(lambda x: x, row[1:])) and
    '/'.join(x for x in row[1:] if x) and
    '/'.join(filter(len, row[1:])) produce
    'Joe/Sally' .

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

Sidebar

Related Questions

Using Python module re, how to get the equivalent of the \w (which matches
I wrote a simple script using python-daemon which prints to sys.stdout : #!/usr/bin/env python
I was hoping to calculate fields using some rather complicated functions, which I don't
I'm looking to store time-based counters using Redis, in a Python project. I want
While Using Python Dictionary DataStructure (which contains key-value pair) if i want to retrieve
Using Python's csv module, is it possible to read an entire, large, csv file
Using Python how do you reduce a list of lists by an ordered subset
Using Python, I want to know whether Java is installed.
Using Python, how does one parse/access files with Linux-specific features, like ~/.mozilla/firefox/*.default ? I've
Using Python I would like to find the date object for last Wednesday. I

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.