9.13 Parse a percentage string as a numerical value
Parse a percentage string as a numerical value.
According to the Titanic disaster survival model result, we want to find the percentage of females among passengers whose survival rates are above 80%. Below is part of the original data:
| Survived | PassengerId | Pclass | Name | Sex | … |
|---|---|---|---|---|---|
| Percent:10.461% | 624 | 3 | Braund, Mr. Owen Harris | male | … |
| Percent:9.108% | 625 | 3 | Cumings, Mrs. John Bradley | male | … |
| Percent:8.891% | 626 | 1 | Heikkinen, Miss. Laina | male | … |
| Percent:50.510% | 627 | 2 | Futrelle, Mrs. Jacques Heath | male | … |
| … | … | … | … | … |
In SPL, number(stringExp,format) function is used to parse string stringExp as a numerical value according to the specified format.
SPL script:
| A | |
|---|---|
| 1 | =file(“titanic.csv”).import@cqt() |
| 2 | =A1.run(Survived=Survived.split(“:”)(2)) |
| 3 | =A2.run(Survived=number(Survived, “0%”)) |
| 4 | =A3.select(Survived > 0.8) |
| 5 | =string(A4.count(Sex==“female”) / A4.len(), “0.000%”) |
A1 Import Titanic data file.
A2 Split Survived field value according to “:” and get the second part.
A3 Use number() function to parse string as a numerical value according to the specified format.
A4 Get passengers whose survival rate is above 80%.
A5 Calculate proportion of female survivors among A4’s passengers.
Execution result:
| Value |
|---|
| 97.619% |
SPL Official Website 👉 https://www.esproc.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.esproc.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/sxd59A8F2W
Youtube 👉 https://www.youtube.com/@esProc_SPL