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

Python: remove brackets of list elements while printing pandas dataframe

I have a pandas dataframe of this kind

d = {'col1': [1, [2,3]], 'col2': [[3,4,5], 4]}
d = pd.DataFrame(d)
print(d)

output:

     col1       col2
0       1  [3, 4, 5]
1  [2, 3]          4

what is the best way to remove the brackets of all the list elements while printing this dataframe?

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 simply convert the column (list) to string and remove the brackets, such that:

for c in d.columns:
    d[c]= d.apply(lambda x: str(x[c]).replace("[",'').replace(']',''), axis=1)

desired result

    col1    col2
0   1       3, 4, 5
1   2, 3    4
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