Python Pandas 的列表操作
Python List Manipulation for Pandas
我有一个正在尝试拆分和操作的列表:
t = ['total percentage,Successful Divert 2935 95.9%,Operational Defect 67 2.2%,MHE Defect 59 1.9%,Load Balancing Defect 1 0.0%']
我很难找出操纵 pandas 的最佳方法。
期望的输出:
t = [['Successful Divert','2935','95.9%'],'[Operational Defect','67','2.2%'],['MHE Defect','59','1.9%'],['Load Balancing Defect','1','0.0%']]
Pandas 输出:
Metrics
Total
Percentage
Successful Divert
2935
95.5%
Operational Defect
67
2.2%
MHE Defect
59
1.9%
Load Balancing Defect
1
0.0%
此解决方案应该适合您
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"])
输出:
我有一个正在尝试拆分和操作的列表:
t = ['total percentage,Successful Divert 2935 95.9%,Operational Defect 67 2.2%,MHE Defect 59 1.9%,Load Balancing Defect 1 0.0%']
我很难找出操纵 pandas 的最佳方法。
期望的输出:
t = [['Successful Divert','2935','95.9%'],'[Operational Defect','67','2.2%'],['MHE Defect','59','1.9%'],['Load Balancing Defect','1','0.0%']]
Pandas 输出:
Metrics | Total | Percentage |
---|---|---|
Successful Divert | 2935 | 95.5% |
Operational Defect | 67 | 2.2% |
MHE Defect | 59 | 1.9% |
Load Balancing Defect | 1 | 0.0% |
此解决方案应该适合您
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"])
输出: