Python Forum
Add column to csv file based on values in existing column - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Add column to csv file based on values in existing column (/thread-7114.html)



Add column to csv file based on values in existing column - Irah - Dec-21-2017

I am new to python and I have a csv file of the following type

id, header1, header2
1, 80-80-20, a
2, 30,b
3,,c
4,60-20,d

Now I would like to separate the values in header1 to new columns where the - is the "delimiter", i.e. it should look like this:

id, header1, header2, header1_1, header1_2, header1_3
1, 80-80-20, a, 80,80,20
2, 30,b,30,,
3,,c,,,
4,60-20,d,60,20,

Any hints on how to solve this using python 3?


RE: Add column to csv file based on values in existing column - SRG - Dec-21-2017

If you post your code, we can offer more succinct guidance. In the absence, however, I can suggest you look at the .split() function of strings. You can split the value in header1 and output it as additional columns in the outfile.


RE: Add column to csv file based on values in existing column - Irah - Dec-22-2017

If I would have code I would have posted it but I had no idea how to even start. But I will try with the split function now.


RE: Add column to csv file based on values in existing column - Larz60+ - Dec-22-2017

Make an attempt, even if you can't get it to work.
We will help you with any problems, but we will not do your homework for you.