user11727742
user11727742

Reputation:

How to split the csv with \n with condition

I have csv and need to split the second column with \n

name,address
711-2880,Mankato\n96522\n(257) 563-7401
971-2880,CA\n965\n(01) 563-7401\nNebraska

Expected Out

name,address
711-2880,Mankato
711-2880,96522
711-2880,(257) 563-7401
971-2880,CA
971-2880,965
971-2880,(01) 563-7401
971-2880,Nebraska

I am able to read the csv and able to convert to dataframe with 2 columns but struggling with seperation \n 971-2880, Nebraska

Upvotes: 0

Views: 57

Answers (1)

adrianp
adrianp

Reputation: 1019

You can use Explode:

df.address = df.address.str.split('\n')
df.explode('address')

You should get:

       name         address
0  711-2880         Mankato
0  711-2880           96522
0  711-2880  (257) 563-7401
1  971-2880              CA
1  971-2880             965
1  971-2880   (01) 563-7401
1  971-2880        Nebraska

Upvotes: 4

Related Questions