Amargitts
Amargitts

Reputation: 17

Split df columns by \n delimiter

I am trying to split my df every 3 rows OR by the \n delimiter. The df looks as follows: enter image description here

I have attempted string replace but I would like to keep the index after the split occurs. Please help

Upvotes: 0

Views: 41

Answers (1)

Gilseung Ahn
Gilseung Ahn

Reputation: 2624

Use str accessor as follows.

for i in range(3, df.columns, 3):
    df.drop(i, axis = 1, inplace = True) # remove column i
    df = pd.concat(df, df[i].str.split('\n', expand = True), axis = 1)

Upvotes: 1

Related Questions