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
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 145 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 186 Aug-19-2022, 11:07 AM
Last Post: dm222
  AttributeError: 'numpy.int32' object has no attribute 'split' rf_kartal 6 594 Jun-24-2022, 08:37 AM
Last Post: Anushka00
  [split] TypeError: 'int' object is not callable flash77 4 941 Mar-21-2022, 09:44 PM
Last Post: deanhystad
  Split single column to multiple columns SriRajesh 1 631 Jan-07-2022, 06:43 PM
Last Post: jefsummers
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 891 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  making variables in my columns and rows in python kronhamilton 2 794 Oct-31-2021, 10:38 AM
Last Post: snippsat
  rows from sql query need to write to a file as columns sjcsvatt 6 1,341 Oct-09-2021, 12:45 AM
Last Post: snippsat
  TypeError: 'DataFrame' object is not callable using Pandas in Python sofiavlachou 1 4,760 Sep-02-2021, 03:24 PM
Last Post: buran
  Merging spreadsheets with the same columns and extracting rows with matching entries johnbernard 3 1,239 Aug-19-2021, 03:08 PM
Last Post: johnbernard

Forum Jump:

User Panel Messages

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