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

Convert each row of a dataframe to list

I have a dataframe like this:

df = pd.DataFrame({'A': ['1', '2', '3'], 'B': ['aa', 'b', 'c']})
   A  B
0  1  aa
1  2  b
2  3  c

I want to convert each row of column B to a list. For example, my desired output is something like this:

   df_new
   A  B
0  1  [aa]
1  2  [b]
2  3  [c]

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

>Solution :

You can use split to do stuff.

import pandas as pd
df = pd.DataFrame({'A': ['1', '2', '3'], 'B': ['a', 'b', 'c']})
df['B'] = df['B'].apply(lambda x: x.split(','))
print(df)
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