Doing Apply(str) to int creates a bunch of \n's? - string

I have a dataframe like this labeled by year:
I would like to change these ints to strs because they are actually categorical variables. However, this is the result:
X_train['YrSold'] = X_train['YrSold'].apply(str)
I would prefer to get rid of all those \n's automatically in the .apply(str) process, as opposed to postprocessing each column via regex. Seems like the latter would have more room for error.

Solution was astype(str) instead of apply(str)

Related

Is it possible to split array in Gherkin to the next line

I have step where I am have String Array, something like this:
Then Drop-dow patient_breed contains ['Breed1', 'Breed2',.... Breed20']
I need to split this text on two lines. I know that in Gherkin there is expression """. I try something like this:
Then Drop-dow patient_breed contains ['Breed1',
"""
'Breed2',.... Breed20']
"""
It didn't help. Is there any solution?
What do you gain by putting this string in your scenario. IMO all you are doing is making the scenario harder to read!
What do you lose by putting this string in your scenario?
Well first of all you now have to have at least two things the determine the exact contents of the string, the thing in the application that creates it and the hardcoded string in your scenario. So you are repeating yourself.
In addition you've increased the cost of change. Lets say we want our strings to change from 'Breedx' to 'Breed: x'. Now you have to change every scenario that looks at the drop down. This will take much much longer than changing the code.
So what can you do instead?
Change your scenario step so that it becomes Then I should see the patient breeds and delegate the HOW of the presentation of the breeds and even the sort of control that the breeds are presented in to something that is outside of Cucumber e.g. a helper method called by a step definition, or perhaps even something in your codebase.
Try with a datatable approach. You will have to add a DataTable argument in the stepdefinition.
Then Drop-dow patient_breed contains
'Breed1'
'Breed2'
...
...
...
'Breed20']
For a multiline approach try the below. In this you will have to add a String argument to the stepdefinition.
Then Drop-dow patient_breed contains
"""
['Breed1','Breed2',.... Breed20']
"""
I would read the entire string and then split it using Java after it has been passed into the step. In order to keep my step as a one or two liner, I would use a helper method that I implemented myself.

How to return string of dict keys in python3 without brackets

I am trying to build an interactive shell-like terminal program in Python3 for a school project.
It should be easily expandable and not rely on non-python-builtin modules.
For this matter, I made a module, which is imported and contains something like this:
commandDictionary={
"command":'''
Information for my program on how to handle command
In multiple lines.''',
}
helpDictionary={
"command":'''
Short Text for the help-command to display
Also in multiple lines.'''
}
What I want to do is to list all keys from helpDictionaryin a string form if help is input.
The output should look like this:
Help
List of available commands:
command1, command2, command3, command4 #Newline after 4 commands.
command5, command6, commandWithALongName, command8
My Problem is, that helpDictionary.keys() returns something like this:
['command1', 'command2']
and I dont want the Brackets nor the ' .
Is this possible?
If you don't want to retain the contents in memory, you can print any iterable you want with an arbitrary separator like this:
print(*helpDictionary.keys(), sep=', ')
If you do want the string for something, use str.join on the separator you want:
s = ', '.join(helpDictionary.keys())
print(s)
Both cases shown above will output the result in essentially arbitrary order because dictionaries use hash tables under the hood. If you want to sort commands lexicographically, replace helpDictionary.keys() with sorted(helpDictionary.keys()).
So, your problem is how to print a list without brackets. There are several solutions.
Traverse the keys: for k in helpDictionary.keys(): print(k)
Or convert the list to a string, then print the mid.
li = list(helpDictionary.keys())
print(str(li)[1:-1])

Having some issues with Perl Splitting and Merging Functions

First and foremost, I'm not familiar with Perl at all. I've been studying C++ primarily for the last 1/2 year. I'm in a class now that that is teaching Linux commands, and we have short little topics on languages used in Linux, including Perl, which is totally throwing me for a loop (no pun intended). I have a text file that contains a bunch of random numbers separated by spaces and tabs, maybe even newlines, that gets read into the program via a filehandle. I'm supposed to write 2 lines of code that split the lines of numbers and merge them into one array, inside of a foreach loop. I'm not looking for an answer, just a nudge in the right direction. I've been trying different things for multiple hours and feel totally silly I can't get it, I'm totally lost with the syntax. Its just a bit odd not working inside a compiler and out of my comfort zone working outside of C++. I really appreciate it. I've included a few photos. Basically, the code we are writing it just to store the numbers and the rest of the program will determine the smallest number and sum of all numbers. Mine is currently incorrect because I'm not sure what to do. In the output photo, it will display all the numbers being entered in via the text file, so you can see them.
Several things to fix here. First of all, please don't post screenshots of your sample data or code, as it makes it impossible to copy and paste to test your code or data. Post your code/data by indenting it with four spaces and a newline preceding the code block.
Add use strict; in your script. This should be lesson 0 in your class. After that add my to all variable declarations.
To populate #all_numbers with contents of each line's numbers, without using push, you can use something like this:
foreach my $line (#output_lines)
{
my #numbers = split /\s/, $line;
#all_numbers = (#all_numbers, #numbers);
}
You say you're "not looking for an answer," so here's your nudge:
You're almost there. You split each line well (using split/\s/) and store the numeric values in #all_numbers. However, notice that each time around in the loop, you replace (using the assignment, #all_numbers = ...) the whole contents of #all_numbers with the numbers you found in the current line. Effectively, you're throwing away everything you've stored from the previous lines.
Instead, you want to add to #all_numbers, not replace #all_numbers. Have a look at the push() function for how to do this.
NB: Your split() call is fine, but it's more customary to use split(' ', $line) in this case. (See split(): you can use a single space, ' ', instead of the pattern, /\s/, when you want to split on any whitespace.)
I hope you need to store the all splitting element into array, so you looking for push function.
foreach $line (#input_lines)
{
push(#all_numbers,split(/\s/,$line));
}
Your problem is, in every iteration, the splitted value is over written in an array not to append together. For example,
#array = qw(one two three);
#array = qw(five four seven);
print "#array";
output is five four seven not the one two three five four seven because this is reinitialize with a new values. You want to append the new values in the array in before or after use unshift or push
for example
#array = qw(one two three);
push(#array,qw(five four seven));
Another way:
my #all_numbers = map { split ' ', $_ } #output_lines;
See http://perldoc.perl.org/functions/map.html

How to save strings in matrixes in matlab

I want to have a matrix/cell, that has strings inside that I can access and use later as strings.
For instance, I have one variable (MyVar) and one cell (site) with names inside:
MyVar=-9999;
site={'New_York'; 'Lisbon'; 'Sydney'};
Then I want to do something like:
SitePosition=strcat(site{1},'_101'}
and then do this
save(sprintf('SitePosition%d',MyVar),);
This doesn't work at all! Is there a way to have strings in a matrix and access them in order to keep working with them if they were a string?
This:
MyVar=-9999; site={'New_York'; 'Lisbon'; 'Sydney'};
SitePosition = strcat(site{1},'_101');
save(sprintf('SitePosition%d',MyVar));
Works fine and yields SitePosition-9999.mat, note the syntax changes in lines 2 and 3.
Is there something else you're expecting?
EDIT: Based on your comment
Check out the documentation for save regarding saving specific variables
New example:
MyVar=-9999;
site={'New_York'; 'Lisbon'; 'Sydney'};
SitePosition = strcat(site{1},'_101');
save(SitePosition,'MyVar');
Creates New_York_101.mat with only the variable MyVar in it.

How to format a flat string with integers in it in erlang?

In erlang, I want to format a string with integers in it and I want the result to be flattened. But I get this:
io_lib:format("sdfsdf ~B", [12312]).
[115,100,102,115,100,102,32,"12312"]
I can get the desired result by using the code below but it is really not elegant.
lists:flatten(io_lib:format("sdfsdf ~B", [12312])).
"sdfsdf 12312"
Is there a better formatting strings with integers in them, so that they are flat? Ideally, using only one function?
You flatten a list using lists:flatten/1 as you've done in your example.
If you can accept a binary, list_to_binary/1 is quite efficient:
1> list_to_binary(io_lib:format("sdfsdf ~B", [12312])).
<<"sdfsdf 12312">>
However, question why you need a flat list in the first place. If it is just cosmetics, you don't need it. io:format/1,2,3 and most other port functions (gen_tcp etc) accept so called deep IO lists (nested lists with characters and binaries):
2> io:format([115,100,102,115,100,102,32,"12312"]).
sdfsdf 12312ok
There is an efficiency reason that io_lib:format returns deep lists. Basically it saves a call to lists:flatten.
Ask yourself why you want the list flattened. If you are going to print the list or send it to a port or write it to a file, all those operations handle deep lists.
If you really need a flattened list for some reason, then just flatten it. Or you can create your own my_io_lib:format that returns flattened lists if you think it important.
(If you only want to flatten the list for debugging reasons then either print your strings with ~s, or create a flattener in an erlang module named user_default. Something like this:
-module(user_default).
-compile(export_all).
%% either this:
fl(String) ->
lists:flatten(String).
%% or this:
pp(String) ->
io:format("~s~n", [String]).
Then you can use fl/1 and print/1 in the Erlang shell (as long as user_default.beam is in your path of course).)

Resources