B-W
B-W

Reputation: 57

Groupby Array CSV

I've got an array which contains an entire column from my csv file which has a lot of duplicates since the csv consist of users their contact info and their current group.

So the users in the CSV are in a group but sometime multiple users are in the same group now the point of this python script is to sort all the users that are in the same group in a row like. I'll update the main thread so it wont confuse more people sorry.

The end goal is of this script will be to put the group in a row together with the phone numbers of the users that are in that group like this.

GroupName, PhoneNumber1, PhoneNumber2, PhoneNumber3, PhoneNumber4, etc
Example:

Name,  email, phonenumber, **group**
name1, name2, number1,     ExampleGroup

So I've tried sorting it by using this: https://wiki.python.org/moin/HowTo/Sorting

And it returns:

['a', ' ', 'E', 'g', 'i', 's', 'm', 'M', 'L', 'o', 'l', 'p', 'S', 'R', 't', 'h', 'e', 'n']

The code I'm using at the moment:

import csv
    with open('Configtool.csv', 'rb') as f:
        reader = csv.reader(f, delimiter=';', quoting=csv.QUOTE_NONE)
        for row in reader:
         groupname = row[5]
         #print groupname
         sortedgroup = list(set(groupname))
         print sortedgroup

Your help is much appreciated,

Upvotes: 1

Views: 57

Answers (1)

haraprasadj
haraprasadj

Reputation: 1087

You are converting to set first. So set method sees a string and it extracts its characters and creates a set out of them.

Try list first and set next - set(list(groupname)). This is if you want to extract the unique items from the list

However sorting might still need applying sorted method.

*I answered but now I am not sure what you need. Please ignore if this does not help.

Upvotes: 1

Related Questions