Python Forum

Full Version: How to split dataframe object rows to columns
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.