Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to text to columns in pandas and create new columns?

I have a csv file as show below, the names column has names separated with commas, I want to spilt them on comma and append them to new columns and create the same csv, similar to the text to columns in excel, the problem is some rows have random number of names.

| Address | Name               |
| 1st st  | John, Smith        |
|2nd st.  | Andrew, Jane, Aaron|

my pandas code look something like this

df1 = pd.read_csv('sample.csv')
df1['Name'] = df1['Name'].str.split(',', expand=True)
df1.to_csv('results.csv',index=None)

offcourse this doesn’t work because columns must be same length as key. The expected output is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

| Address | Name  |     |      |
| 1st st  | John  |Smith|      |
|2nd st.  | Andrew| Jane| Aaron|

>Solution :

count the max number of commas, then accordingly assign to new columns.

max_commas = df['name'].str.split(',').transform(len).max()
df[[f'name_{x}' for x in range(max_commas)]] = df['name'].str.split(',', expand=True)

input df:

      col                        name
0  1st st                 john, smith
1  2nd st          andrew, jane, aron
2  3rd st  harry, philip, anna, james

output:

      col                        name  name_0  name_1 name_2 name_3
0  1st st                 john, smith    john   smith   None   None
1  2nd st          andrew, jane, aron  andrew    jane   aron   None
2  3rd st  harry, philip, anna, james   harry  philip   anna  james
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading