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 a calculated column charles986 1 174 Jun-13-2024, 05:31 PM
Last Post: deanhystad
  adding a calculated column charles986 1 179 Jun-13-2024, 02:27 PM
Last Post: deanhystad
  Adding PD DataFrame column bsben 2 472 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 574 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  Copy Paste excel files based on the first letters of the file name Viento 2 625 Feb-07-2024, 12:24 PM
Last Post: Viento
  Help copying a column from a csv to another file with some extras g0nz0uk 3 632 Feb-01-2024, 03:12 PM
Last Post: DeaD_EyE
  Converting column of values into muliple columns of counts highland44 0 388 Feb-01-2024, 12:48 AM
Last Post: highland44
  Python code to set column width 1418 11 2,017 Jan-20-2024, 07:20 AM
Last Post: Pedroski55
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 863 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Returning Column and Row Data From Spreadsheet knight2000 0 536 Oct-22-2023, 07:07 AM
Last Post: knight2000

Forum Jump:

User Panel Messages

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