Search code examples
excelpython-3.xpandascsvdata-analysis

converting comma separated data of excel file into new line using python, pandas


I have an excel file with data as

StudentId    Details
1234         John, Texas, United States
9887         Roma, Moscow, Russia

I want to convert it into the following format, such that:

StudentId    Details
1234         John
             Texas
             United States 

9887         Roma
             Moscow
             Russia

I am using Pandas for this purpose but not getting the results

for i in range(len(df['Details'])):
    df['Details'][i]=df['Details'][i].replace(',','\n')

I am using somewhat this kind of logic


Solution

  • Read it in with read_fwf() then split up that column later:

    df['Details'].str.split(',')