2021年4月22日星期四

Separate column data based on comma characters using pandas regex

I have a dataframe like as shown below

df = pd.DataFrame({'val': ['V583 ,ATTENTION, PRIMARY','Y9207,INDOOR LIVING, sEcondary',' z526, liver,primary ','12345678, test, secondary',',project,']})  

I would like to split/segregate column val data based on comma character.

For instance, All characters before 1st comma should go to first column Similarly, All characters after 1st comma and before 2nd comma should go to second column likewise, All characters after 2nd comma and before 3rd comma should go to third column

I tried the below

df['val'].astype(str).str.extract(r'\s*([a-zA-Z0-9\s]*)',expand=True)  

I expect my output to be like as shown below

enter image description here

https://stackoverflow.com/questions/67223873/separate-column-data-based-on-comma-characters-using-pandas-regex April 23, 2021 at 12:05PM

没有评论:

发表评论