Say I’m importing a .csv table where headers are:
Column 2 - Data_value
Column 11 - Series_title_2
and I’m printing out the values from column 2 where column 11 contains “Mining”, as follows:
Now, how can I concatenate more filters? I.e. by also adding “Actual” from column 12 - Series_title_3?
I’ve tried a for loop inside the for loop to no avail.
Thanks
Hi @glv, thanks for the references.
I checked the ‘Table’ ref page with all the methods inside out for hours yesterday, and the Data tutorial provided is identical to the Data chapter from the ‘Learning Processing Morgan Kaufmann Series’ by Daniel Shiffman book, which I’m using as a textbook to learn Processing.
As far as I know, getString() will give me one unique value when row/column is specified, but the result of my code so far is a list of 300 values, which I need to further reduce to 44 by introducing additional filters (similar to concatenating filters in Excel).
Right now, I’m able to apply the single filter of “Mining”, but I need to add the “Total earnings” and “Actual” ones to get the values from column C below.
Are you suggesting running that for loop 3 times with single filters each then use equals to compare the results? Sorry can’t seem to get the idea there
I was trying this… but doesn’t seem to work (below full code). It made sense to me to be restricting the output more and more by adding additional loops/conditions.
mmm… I’m afraid this doesn’t work either… or I don’t know how to apply it
As per the image below with a simpler table, the output of println() should be just those 13 values in column 3, that is:
147.870064
148.915685
143.481452
145.689559
139.114093
143.738115
144.76762
140.216363
153.26737
145.279694
141.911001
154.31287
149.699332
There is not a single string value from columns Series_title_1,2 and 3 which is common between those columns, so I fail to see how s1.equals(s2) would work??
Aaaaaaaahhhhh, got it! I’m so used to the Excel rationale when it comes to filters that didn’t see the way of thinking in programming is different lol
Ok, so my code is correct till ‘println(income)’; now I get the target 13 values
Now, when I try to store those values in the table by using ‘table.setFloat’ then use those income values as the height for a bar chart is when there’s something missing .
Table table = loadTable("data.csv","header");
for(int i = 0; i < table.getRowCount(); i++) {
//iterate over all the table rows
TableRow row = table.getRow(i);
//define variables to retrieve the column headers I'm interested to filter by
String s1 = row.getString("Series_title_1");
String s2 = row.getString("Series_title_2");
String s3 = row.getString("Series_title_3");
//define the target variable from where I need to get my results
float income= row.getFloat("Data_value");
//define filtering condition
if(s1.equals("Total earnings") && s2.equals("Mining") && s3.equals("Actual")) {
println(income);
table.setFloat(i,"Data_value",income);
}
//draw rectangles whose height equals income
//I think the problem is here when defining the bar widths?!?
float w = width/income;// or... table.getRow(i).getFloat("Data_value");
fill(0);
stroke(255);
rect(w*i,height-income,w,income);
}
1 Cool! @glv I’m getting closer I followed those points and managed to get the counter working and printing the 1,2,3 …13 to account for all the hits.
3 My view is that you need to draw the bar inside the if in order to obtain just 13 bars, otherwise the filter would not apply.
2 What I’m struggling with now is the rectangle spacing, that is how to obtain the max(count) to divide the width always by 13. Right now as count is always increasing so is the denominator of ‘w’.
int count = 0; //var to count the final number of filtered values
Table table = loadTable("data.csv","header");
for(int i = 0; i < table.getRowCount(); i++) {
//iterate over all the table rows
TableRow row = table.getRow(i);
//define variables to retrieve the column headers I'm interested to filter by
String h1 = row.getString("Series_title_1");
String h2 = row.getString("Series_title_2");
String h3 = row.getString("Series_title_3");
//define variables to retrive the text filters we're looking for
String filter1 = "Total earnings";
String filter2 = "Mining";
String filter3 = "Actual";
//define filtering condition
if(h1.equals(filter1) && h2.equals(filter2) && h3.equals(filter3)) {
//define the target variable from where I need to get my results
float income = row.getFloat("Data_value");
//increment the counting
count++;
//draw rectangles whose height equals income
float w = width/count;
fill(0);
stroke(255);
rect(0,height,w,-income); //I know 0 is not correct but w*i doesn't work so one at a time :)
println(income + " / " + count + " / " + w);
}
}
Now still struggling with the ‘w’… had to use a fixed one (5) temporarily for visibility.
This below doesn’t work as count increases therefore ‘w’ decreases. I need to pick the maximum value of ‘count’, which is 13, and divide width by it, am I right?
I tried max(count) but that’d mean turning count into an array and doesn’t take me anywhere.
float w = width/count;
Also tried this with no luck.
IntList count_values = new IntList();
count_values.append(count);
int max_count = count_values.max();
//draw rectangles whose height equals income
float w = width/max_count;
size(600, 200);
int max = 70; // Try different values
for(int i = 0; i< max; i++)
{
int x = i;
int y = 0;
float w = float(width)/max; // I need floating point math here. Try with the float!
float h = random(50, 170); // Random elements... this could have been from array or table
stroke(0);
rect(x*w, y, w, h);
}
Take a look at the syntax and parameters in the reference:
Explore (play with different values) and understand this first and then integrate your version into your code.
What I’m trying is to not have to use a hardcoded value, but ‘max’ being the maximum value of my counter, as per below, so if I use another filter on the table and the max of counter is 7 don’t have to modify the code.
Does it make sense?