3

I am familiar with basic concepts of reading and writing a csv file in python. But I am stuck to make a logic for this problem. I think GROUP BY can solve my problem but how one can do in python

Category         Data
A                Once upon a time.
A                There was a king.
A                who ruled a great and glorious nation.
B                He loved each of them dearly. 
B                One day, when the young ladies were of age to be married. 
B                terrible, three-headed dragon laid. 
C                It is so difficult to deny 
C                the reality

I want to make logic for such an output that data with category A merges to one row and same for category B and C like this.

Category         Data
    A                Once upon a time. There was a king. who ruled a great and glorious nation.
    B                He loved each of them dearly. One day, when the young ladies were of age to be married. terrible, three-headed dragon laid. 
    C                It is so difficult to deny the reality

Please if anyone of you can help me out with this logic I would appreciate his effort.

1

2 Answers 2

3

With pandas library you can use groupby and make a custom aggregate function that just concatenates each category's Data

>>> import pandas as pd
>>> data = [['A', 'Once upon a time.'], ['A', 'There was a king.'], ['A', 'who ruled a great and glorious nation.'], ['B', 'He loved each of them dearly. '], ['B', 'One day, when the young ladies were of age to be married. '], ['B', 'terrible, three-headed dragon laid. '], ['C', 'It is so difficult to deny '], ['C', 'the reality']]
>>> df = pd.DataFrame(data=data, columns=['Category','Data'])
>>> df
  Category                                               Data
0        A                                  Once upon a time.
1        A                                  There was a king.
2        A             who ruled a great and glorious nation.
3        B                     He loved each of them dearly.
4        B  One day, when the young ladies were of age to ...
5        B               terrible, three-headed dragon laid.
6        C                        It is so difficult to deny
7        C                                        the reality
>>> df.groupby('Category').agg({'Data': lambda x : ' '.join(x)})
                                                       Data
Category
A         Once upon a time. There was a king. who ruled ...
B         He loved each of them dearly.  One day, when t...
C                   It is so difficult to deny  the reality
Sign up to request clarification or add additional context in comments.

1 Comment

Are the other columns in the csv also distinct? Meaning if a second column Data2 were in your csv are the records for each Category the same or different on every line?
2

itertools.groupby can help (assuming the letters in your first row are ordered):

from itertools import groupby
from io import StringIO

text = '''Category         Data
A                Once upon a time.
A                There was a king.
A                who ruled a great and glorious nation.
B                He loved each of them dearly.
B                One day, when the young ladies were of age to be married.
B                terrible, three-headed dragon laid.
C                It is so difficult to deny
C                the reality
'''

with StringIO(text) as file:
    next(file)  # skip header
    rows = (row.split('                ') for row in file)
    for key, items in groupby(rows, key=lambda x: x[0]):
        phrases = (item[1].strip() for item in items)
        print(key, ' '.join(phrases))

which gives:

A Once upon a time. There was a king. who ruled a great and glorious nation.
B He loved each of them dearly. One day, when the young ladies were of age to be married. terrible, three-headed dragon laid.
C It is so difficult to deny the reality

if your data is in a file, you have to replace the with StringIO(text) as file: above with:

with('textfile.txt') as file:
    # do stuff as above with file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.