I'm using some pyspark.sql.functions:
print(ratings.select(mean('rating')).take(1))
print(ratings.select(stddev('rating')).take(1))
The output is:
[Row(avg(rating)=3.581564453029317)]
[Row(stddev_samp(rating,0,0)=1.1171018453732544)]
How can I extract the value so that I can assign it to a variable, e.g.
mean_ratings = ratings.select(mean('rating'))
Take returns a list of Rows. Index into the list to get the first row, then pull out the field you are looking for:
mean_ratings = ratings.select(mean('rating')).take(1)[0]['avg(rating)']
Related
I am trying to grab data from an excel spread sheet and grab only the information from cells that match a string. Eg. if cell A10 contains the word 'Canada' it should return that cell.
I have tried using strcmp(https://www.mathworks.com/help/matlab/ref/strcmp.html) to check if string in argument 1 is contained in a cell array containing many strings, the second argument
[num,txt,raw] = xlsread('\\Client\C$\Users\Fish\Desktop\dataset\dataset.csv');
mytable = cell(raw);
for i = 1:54841
array_index = i;
string_index = mytable(i,2);
string_eastern = {'Canada', 'Ontario'};
if strcmp(string_index,string_eastern);
fprintf('%d\n',array_index)
end
end
In the above example if my string_eastern only contains one element, say 'Canada', it will return the index value of every instance of 'Canada'. If I add more elements I expect it would return index values for every instance where string_index would match with a string contained in string_eastern. However I get no results at all if I add more elements.
Pretty much I wanted to check my string_index agaisnt string_eastern, if the values match then I want it to return that cell value. This works when string_eastern is only 1 element but does not work with more than 1.
To access cell contents, use the curly brackets {}. So if I wanted to access the first element of my cell, I would say this:
string = cell{1};
Read more on the MATLAB Documentation about cells to learn more and to answer any of your further questions.
I have a cell array with values similar to the following one
13:41:54.879
I would like to extract only 13:41 part of the given value and mitigate the rest. I tried various combinations of extractBefore() and extractAfter() but couldn't get it.
You can use a regular expression to match the pattern "digits, colon, digits":
c = {'13:41:54.879', '1:22:33.45679'};
result = regexp(c, '\d+:\d+', 'match', 'once');
gives
result =
1×2 cell array
{'13:41'} {'1:22'}
By using the listdlg its possible to select the file from list, but it returns the respective index as a output but not the name(string of selected entity)of the selection. how can one get the name of the selected file in output??'
for example
[Selection, ok] = listdlg(Name,Value,...);
% selection is nothing but a index of selected entities.
The dialog box is filled with a cell array provided as the value of the ListString parameter. The result of the call to listdlg is the index into this cellarray.
Consider the following code:
filelist=dir("/home");
S={filelist.name};
[Selection,ok]=listdlg('ListString',S,'SelectionMode','single');
if (ok) filename=cell2mat(S(Selection)) endif
If the selection of the item user1 was made it should output
filename = user1
Update
When SelectionMode is multiple, you could use celldisp(S(Selection)). To extract individual items use S{Selection(i)} where i ranges from 1 to length(Selection).
filelist=dir("/home");
S={filelist.name};
[Selection,ok]=listdlg('ListString',S,'SelectionMode','multiple');
if (ok)
for i=1:length(Selection)
disp(S{Selection(i)})
end
endif
Hi I want to search a cell array for any elements containing the letter 'x'. I can delete a cell element by doing the following:
mycell(3) = []
but trying to search through the elements id the difficult part. I am using:
offending_cell = strcmp('x', mycell);
however this is just picking out all elements regardless of x appearing in them. Anyone have any suggestions?
There you go:
ind = (~cellfun('isempty',(regexp(mycell,'x'))));
This gives logical indices for cells that contain 'x'. If you want to delete those cells:
mycell(ind) = [];
The problem with you apporach was that strcmp looks for exact matching, not if the string contains a given character.
I have a form with a table in it. In each row is a table cell with a hidden input item with the name of it starting with "hf_id_" followed by a number so that row 1's field has a name of "hf_id_1", row 2 is "hf_id_2" and so on. I need to search all of these fields for a particular value but I'm not quite sure how to get to the hidden fields. I know how to get to them when the full name is known but in this case I'm not sure if there's a way to get an array of these where name starts with "hf_id_". Thanks.
You can search elements with ^ (starting with) and $ (ending with), example:
$('input[name^="hf_id_"]');
So you can get all those elements like:
var elements = $('input[name^="hf_id_"]');
And you can iterate over them to search for a particular value like:
$('input[name^="hf_id_"]').each(function(){
if ($(this).val() === 'search value here')
{
// found..........
}
});
Or you could simply use
$('input[type="hidden"]');