Search code examples
pythonpandascsvexport-to-csv

How to split CSV column data into two colums in Python


I have the following code (below) that grabs to CSV files and merges data into one consolidated CSV file.

I now need to grab specific information from one of the columns add that information to another column.

What I have now is one output.csv file with the following sample data:

ID,Name,Flavor,RAM,Disk,VCPUs
45fc754d-6a9b-4bde-b7ad-be91ae60f582,customer1-test1-dns,m1.medium,4096,40,2
83dbc739-e436-4c9f-a561-c5b40a3a6da5,customer2-test2,m1.tiny,128,1,1
ef68fcf3-f624-416d-a59b-bb8f1aa2a769,customer3-test3-dns-api,m1.medium,4096,40,2

What I need to do is open this CSV file and split the data in the Name column across two columns as followed:

ID,Name,Flavor,RAM,Disk,VCPUs,Customer,Misc
45fc754d-6a9b-4bde-b7ad-be91ae60f582,customer1-test1-dns,m1.medium,4096,40,2,customer1,test1-dns
83dbc739-e436-4c9f-a561-c5b40a3a6da5,customer2-test2,m1.tiny,128,1,1,customer2,test2
ef68fcf3-f624-416d-a59b-bb8f1aa2a769,customer3-test3-dns-api,m1.medium,4096,40,2,customer3,test3-dns-api

Note how the Misc column can have multiple values split by one or multiple -.

How can I accomplish this via Python. Below is the code I have now:

import csv
import os
import pandas as pd

by_name = {}
with open('flavor.csv') as b:
    for row in csv.DictReader(b):
        name = row.pop('Name')
        by_name[name] = row

with open('output.csv', 'w') as c:
    w = csv.DictWriter(c, ['ID', 'Name', 'Flavor', 'RAM', 'Disk', 'VCPUs'])
    w.writeheader()

    with open('instance.csv') as a:
        for row in csv.DictReader(a):
            try:
                match = by_name[row['Flavor']]
            except KeyError:
                continue

            row.update(match)

            w.writerow(row)


Solution

  • Try this:

    import pandas as pd
    
    df = pd.read_csv('flavor.csv')
    df[['Customer','Misc']] = df.Name.str.split('-', n=1, expand=True)
    df
    

    Output:

    
                                         ID                     Name     Flavor   RAM  Disk  VCPUs   Customer           Misc
    0  45fc754d-6a9b-4bde-b7ad-be91ae60f582      customer1-test1-dns  m1.medium  4096    40      2  customer1      test1-dns
    1  83dbc739-e436-4c9f-a561-c5b40a3a6da5          customer2-test2    m1.tiny   128     1      1  customer2          test2
    2  ef68fcf3-f624-416d-a59b-bb8f1aa2a769  customer3-test3-dns-api  m1.medium  4096    40      2  customer3  test3-dns-api