Python Forum
How to split dataframe object rows to columns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to split dataframe object rows to columns
#1
Hi,
I have below data frame (multiple rows): I want to split rows at dilimiter to columns


df_obj:
20;12;AA;2020/11/05 10:23:13;2020/11/05 10:25:13;12;LL
20;12;PT;2020/11/05 11:18:13;2020/11/05 11:23:43;34;KLY
20;12;BN;2020/11/09 11:18:13;2020/11/05 11:23:73;4;KMLY
34;2;DG;2020/11/11  11:18:13;2020/11/11 11:23:73;4;KY
I want to split each row at ";" into columns

My desiredoutpur:

df_out:
20 12 AA 2020/11/05 10:23:13 2020/11/05 10:25:13 12 LL
20 12 PT;2020/11/05 11:18:13 2020/11/05 11:23:43 34 KLY
20 12 BN 2020/11/09 11:18:13 2020/11/05 11:23:73 4 KMLY
34 2 DG 2020/11/11  11:18:13 2020/11/11 11:23:73 4 KY
I use the below line. It gives the "data frame object has no attribute split".

df_obj.split(";")
any method to split data frame object rows.
Reply
#2
You can convert your data in to a string first, then split it or iterate over it. You basically just want to change the ";"s to spaces.
old_data_string = str(df_obj)
new_data_string = ""

for character in old_data_string:
    if character == ";":
        new_data_string.append(" ")
    else:
        new_data_string.append(character)
Full disclosure, I don't know if the "/"s in your data will screw up anything, and this not the most pythony solution, but it might get you started.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split single column to multiple columns SriRajesh 1 145 Jan-07-2022, 06:43 PM
Last Post: jefsummers
  AttributeError: 'Message' object has no attribute 'split' helpme1 3 2,203 Dec-14-2021, 07:20 AM
Last Post: alexaminar
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 322 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  making variables in my columns and rows in python kronhamilton 2 401 Oct-31-2021, 10:38 AM
Last Post: snippsat
  rows from sql query need to write to a file as columns sjcsvatt 6 644 Oct-09-2021, 12:45 AM
Last Post: snippsat
  TypeError: 'DataFrame' object is not callable using Pandas in Python sofiavlachou 1 2,839 Sep-02-2021, 03:24 PM
Last Post: buran
  Merging spreadsheets with the same columns and extracting rows with matching entries johnbernard 3 755 Aug-19-2021, 03:08 PM
Last Post: johnbernard
  Summing up rows and columns plumberpy 3 832 Aug-18-2021, 05:46 AM
Last Post: naughtyCat
  Apply fillna to multiple columns in dataframe rraillon 2 661 Aug-05-2021, 01:11 PM
Last Post: rraillon
  Partial Matching Rows In Pandas DataFrame Query eddywinch82 1 785 Jul-08-2021, 06:32 PM
Last Post: eddywinch82

Forum Jump:

User Panel Messages

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