Python Forum
How to split dataframe object rows to columns - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to split dataframe object rows to columns (/thread-30905.html)



How to split dataframe object rows to columns - Mekala - Nov-12-2020

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.


RE: How to split dataframe object rows to columns - michael1789 - Nov-12-2020

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.