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 List Manipulation for Pandas

I have a List im trying to split and manipulate :

t = ['total percentage,Successful Divert 2935 95.9%,Operational Defect 67 2.2%,MHE Defect 59 1.9%,Load Balancing Defect 1 0.0%']

Im having a hard time figuring out the best way to manipulate for pandas.

Desired output:

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

t = [['Successful Divert','2935','95.9%'],'[Operational Defect','67','2.2%'],['MHE Defect','59','1.9%'],['Load Balancing Defect','1','0.0%']]

Pandas output:

Metrics Total Percentage
Successful Divert 2935 95.5%
Operational Defect 67 2.2%
MHE Defect 59 1.9%
Load Balancing Defect 1 0.0%

>Solution :

This solution should works for you

t = [
    "total percentage,Successful Divert 2935 95.9%,Operational Defect 67 2.2%,MHE Defect 59 1.9%,Load Balancing Defect 1 0.0%"
]
t2 = [x.split(" ") for x in t[0].split(",")[1:]]
t3 = [(" ".join(x[:-2]), x[-2], x[-1]) for x in t2]
pd.DataFrame(t3, columns=["Metrics", "Total", "Percentage"])

Output:

Output:

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