Python Forum
Add column to csv file based on values in existing column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add column to csv file based on values in existing column
#1
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?
Reply
#2
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.
Reply
#3
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.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create a new subclass in a Python extension based on an existing class voidtrance 6 1,441 Mar-25-2025, 06:37 PM
Last Post: voidtrance
  Camelot not able to change column header shortmeister1 2 794 Mar-22-2025, 04:47 AM
Last Post: Pedroski55
  Transform 3 Columns into Single Column DaveG 9 3,598 Mar-19-2025, 03:46 AM
Last Post: robbert23
  renaming a column without a name in a dataframe Carbonpony 2 987 Jan-23-2025, 08:20 AM
Last Post: Carbonpony
  Updating column name with translation bobbydave 0 1,183 Sep-17-2024, 03:40 PM
Last Post: bobbydave
  adding a calculated column charles986 1 911 Jun-13-2024, 05:31 PM
Last Post: deanhystad
  adding a calculated column charles986 1 695 Jun-13-2024, 02:27 PM
Last Post: deanhystad
  Adding PD DataFrame column bsben 2 1,341 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 2,019 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  Copy Paste excel files based on the first letters of the file name Viento 2 1,600 Feb-07-2024, 12:24 PM
Last Post: Viento

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020