Skip to content

Latest commit

 

History

History
31 lines (25 loc) · 1.54 KB

example-06.md

File metadata and controls

31 lines (25 loc) · 1.54 KB

Example #6 (Titanic Survivor Statistics)

First few rows of the titanic dataset looks like this

Survived "0" means the person didn't survive and "1" means that the person survived. The question posed is what is the percentage of people in each class that survived and died. "Pclass" column depicts the class of the person boarded. "1" means first class, "2" means second class and "3" means third class.

The following code uses Squirrel to solve it.

Table titanic = DataAcquisition.LoadCSV(@"titanic.csv");
var classes = titanic.SplitOn("Pclass");
Table result = new Table ();
string[] pClasses = {"1","2","3"};
foreach(pClass in pClasses)
{
    Dictionary<string,string> thisRow = new Dictionary<string,string> ();
    thisRow.Add("Class",pClass.ToString());
    thisRow.Add("Died(%)", (100 * classes[pClass].GetPercentage("Survived", "0")).ToString());
    thisRow.Add("Survived(%)", (100 * classes[pClass].GetPercentage("Survived", "1")).ToString());
    result.AddRow(thisRow);
}
result.PrettyDump();

This produces the following output

This uses the function GetPercentage which returns the percentage of values of the given column that match with the given value. For example here it returns the percentage of values in "Survived" column that is "0" for calculating "Died(%)" column.