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
  This result object does not return rows. It has been closed automatically dawid294 6 1,032 Mar-30-2024, 03:08 AM
Last Post: NolaCuriel
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,636 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,435 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,878 Dec-12-2022, 08:22 PM
Last Post: jh67
  Check DataFrames with different sorting in columns and rows Foxyskippy 0 778 Nov-19-2022, 07:49 AM
Last Post: Foxyskippy
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 832 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,712 Aug-19-2022, 11:07 AM
Last Post: dm222
  AttributeError: 'numpy.int32' object has no attribute 'split' rf_kartal 6 4,395 Jun-24-2022, 08:37 AM
Last Post: Anushka00
  [split] TypeError: 'int' object is not callable flash77 4 2,759 Mar-21-2022, 09:44 PM
Last Post: deanhystad
  Split single column to multiple columns SriRajesh 1 1,325 Jan-07-2022, 06:43 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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