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
  Adding PD DataFrame column bsben 2 299 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 266 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  Copy Paste excel files based on the first letters of the file name Viento 2 422 Feb-07-2024, 12:24 PM
Last Post: Viento
  Help copying a column from a csv to another file with some extras g0nz0uk 3 456 Feb-01-2024, 03:12 PM
Last Post: DeaD_EyE
  Converting column of values into muliple columns of counts highland44 0 251 Feb-01-2024, 12:48 AM
Last Post: highland44
  Python code to set column width 1418 11 1,168 Jan-20-2024, 07:20 AM
Last Post: Pedroski55
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 728 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Returning Column and Row Data From Spreadsheet knight2000 0 434 Oct-22-2023, 07:07 AM
Last Post: knight2000
  Find a string from a column of one table in another table visedwings049 8 1,166 Sep-07-2023, 03:22 PM
Last Post: deanhystad
  DF value by Column... not sure what to call it tester_V 2 563 Aug-26-2023, 06:56 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

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