Adding numbers from 3 different CSV columns - c

I am newer to programming and I am trying to figure out how I can add all of the numbers from the B column together and all of the C column together and the D column together with those variables named x y and z respectively. I have been searching everywhere for an answer. All I have gotten is to read the first line of the csv file.
This is a screenshot of part of the CSV file:

Once you've read it in, you need to somehow split it up into actual columns. Check this function, that should help.
strtok()
Then you need to store it in some kind of array. Then index which elements in the array you want, and process them by using a cumulative sum. This might require a while loop until you've reached the last line of the file.
I will leave the actual code to you, but this should get you going.

Related

Setting one CSV as an array to compare data from another CSV

I am new to Python and am over complicating the coding on a project so I am starting with much smaller data sets in order to learn the process. My boss is having me compare two CSV files. The first CSV only contains the data 1,2,3,4,5,6 all in a single column. He wants me to set this CSV file as an array so I can compare the second CSV against it. The second CSV contains the data 3,5,6 all in a single column. The code should result in a print out of 1,2,4 as it is the only data not found in both CSV files.
I originally tried to write a code to import both CSV files and compare data without setting it as an array but this did not work so the first CSV file needs to be set as an array. The problem is I am not sure exactly how to do this with an array. This is what I have so far, any help anyone could give me would be greatly appreciated. I have been working on this project for a week now and am at a total loss, even with this simplified form.
import csv
temp_list = []
with open('1.csv','rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
In terms of psuedo-code, what you need to do here is import both csv files into two separate arrays, Array A and Array B for example.
Now what you need to do is compare each index position in one array, to each index position in the other array.
You need to create a nested loop, where the outer loop will choose an index position in A and then inner loop chooses a position in B.
After you check one index in A with each position in B, and no positions are the same, I suggest adding this value into a third array, C. You can check which positions are the same by using a boolean flag. When your code is done, C will have any values that don't exist in both A and B.
I suggest following these tutorials to learn more about python syntax:
https://www.w3schools.com/python/
Good luck

Excel: How to use an Array to concatenate strings that are next to a criteread section (If function)

Good Day Everyone
I have an Excel sheet as bellow in two columns. I know how to use an array if function with sum to add up all values next to a column that has met the criterea as in {=sum(if(A1:A5="YES",B1:B5)} but how to go about doing it with strings and concatenate so that they show the result as below
Thanks for Any Help
The Excel array
Edit: I know there is a easy VBA solution, but was wondering if there is an excell way since there is an easy solution if it was values and not strings.
If I'm assuming you don't want the relatively easy VBA solution where you recurse through the initial list and concatenate the answers then you could put the following into B8:
=IFERROR(INDEX(Sheet1!$A$1:$A$6,SMALL(IF(Sheet1!$B$1:$B$6=$A8,ROW(Sheet1!$B$1:$B$6)-ROW(Sheet1!$B$1)+1),COLUMNS($B1:B1))),"")
You'd have to drag it across as it'll put them in separate columns but then you should be able to concatenate them into a single column afterwards.
You have to have Ctrl and Shift held down when inputting as it's an array formula.
Activate iteration in the options. The maximum number of iterations must be equal to or greater than the number of lines.
Add =IF(D1=100;1;D1+1) to cell D1
This is the formula in cell E1 for the Stark houses:
=IF(D1=1;"";IF(D1-1>COUNTIF(B:B;"Stark");E1;E1&INDEX(A$1:A$100;SMALL(IF(B$1:B$100="Stark";ROW($1:$100));D1-1))))
(I hope that I got the English names of the functions correctly.)
I missed one information: You must enter the function as an array function.
Thanks go to http://www.excelformeln.de/formeln.html?welcher=155
Was really Strugling to get it right and eventualy got an answer from a stackoverflow question on how to concatenate whole columns.
Sorted the data via B and then used =TRANSPOSE(A4:A6)
Then Used F9 on the function to get this line ={"Mansion1","Mansion 2","House 3"}
Copied and Concatenate and just remove the {
But this was not ideal as the data set I want to use this on is 2500+ lines :(
Manualy did it though :( :( but will try the above answers for future use.

How can I printf first in columns then in lines?

maybe this question seems stupid... I would like to fprintf a dataset formatted in rows and columns. I know the procedure so far
for(i=0;i<number_of_rows;i++)
{
for(j=0;j<number_of_columns;j++)
fprintf(file,"%g\t",array[i][j]);
fprintf(file,"\n");
}
What essentially does this code is to fill up first the rows and then the columns. I have datasets that are created by columns. Thus, I want to fill up every column before I pass to the next one. I don't know before the runtime the length of the output data in order to make a proper 2D array. And I need to print out first a whole column, then the other. How can I do this?
And I need to print out first a whole column, then the other. How can I do this?
That would involve appending to a previously printed line. And, as you may know, adding content into a file involves rewriting the entire rest of the file. You can probably guess that would be incredibly inefficient and also complicated to implement.
I don't know before the runtime the length of the output data in order to make a proper 2D array
If your problem is the length of the data, then I assume that by proper 2D array you mean that you intend to pad each cell of a column to be the same width.
In that case, I recommend not to waste time on the idea of writing column first. Instead, calculate the widths at runtime. First sprintf into a matrix of strings and calculate the length of each cell. Then print the matrix of strings using the calculated lengths.

How to Write multiple Arrays in excel using Matlab

I have list of values save in variables like. A= 1,2,3,4,5,6,7 and B = 4,5,6,73,2,3,2 //This may be Array or column. Easy one will be preffered
Note A and B values will be dynamic which will be get through some function.
I want to save A's and B's Values in Excel Sheet, like A's values in First column and B's values in 2nd Column.
I read about xlswrite but did not find any scenerio as i require.
And when They are saved then Again want to read them, and to save them in some other variables like C and D. I want to save Data of first column in C and second column data in D.
I read about C = xlsread('filename') but problem with it is. This save all the values in one variable. like if Excell have two columns. Those both will be saved in one variable C. But my requirment is one colum in one varibale and so on.
Take a look at the Documentation
Syntax for writing to excel file
xlswrite(filename,A,sheet,xlRange)
Code:
%// As your variable is a row vector, it is transposed to column vector
%// before writing, as you preferred
xlswrite('outputFileName.xlsx',[A.',B.'],1,'D2')
Note: Make sure that the excel file is closed while writing
Syntax for reading from excel file
num = xlsread(filename,sheet,xlRange)
Code:
%// reading the range into one temporary variable
temp = xlsread('outputFileName.xlsx',1,'D2:E7')
C = temp(:,1)
D = temp(:,2)
I know English is not your primary language, but please try to be as gramatically correct as you can, it will make your questions easier to understand, and easier to answer to.
As for your question, I think you want to use the additional parameter in the functions you found to specify the columns you are writing to:
xlswrite('sheet.xls', A, 'E3')
will write the data of A on the cells E3, ..., E9.
And when you want to retrieve it, you do the same:
C = xlsread('sheet.xls', 'E3-E9')
This will read E3 to E9 cells and put the value in C.

Multiple datasets in gnuplot: difference in behavior of index with iteration vs range

I think I understand from the behavior how these two plot commands differ, but I don't really understand why they differ. That is, I didn't expect there to be a difference. The two cases are:
plot for [i=0:3] 'ctg-y2.dat' index i using 2 title columnheader(2) with lines
and
plot 'ctg-y2.dat' index 0:3 using 2 title columnheader(2) with lines
(the example datafile is http://gnuplot.cvs.sourceforge.net/viewvc/gnuplot/gnuplot/demo/ctg-y2.dat)
The first one does what I would expect: for each of four datasets in the file, read a column header from the first line of the dataset, and plot the remaining data. The second one does something rather different: it does not read a column header for any dataset but the first, and it seems to plot all the data as if it were part of one dataset. The result is a mess, since the implicit x values don't match up correctly.
The description of index in the manual doesn't talk about this behavior of using a range with index, as near as I can tell. Is it documented somewhere? Is this a bug? Am I doing something stupid?
I have never used the index before, but if I understand it correctly, it seems to merge all indexed data sets to a single set. This is why the lines all appear in the red color. However, if plotted against the column index and not a given index (e.g. using 2 vs using 1:2) it seems to fall back to index 1. This is due to the fact that the first line cannot be interpreted (since it is a title).
The issue with the column headers seems to be working ok, since gnuplot expects to have just a single dataset, which is combined with the keyword index, and therefor does not expect to have multiple column headers.
Since you already solved your problem with the iterations there is no need for further suggestions. I think this is exactly how you should plot your data.

Resources