Home > Software engineering >  Python List Manipulation for Pandas
Python List Manipulation for Pandas

Time:04-02

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:

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%

CodePudding user response:

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:

  • Related