Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
good I'm doing a program which calculates genetically that man inside a list is the fittest to survive but when people eliminate by the command. sends me this error pop can not concatenate 'str' and 'int' objects
how can fix this?
Somewhere in your Python script you have two values being added together, where one value is a string and the other value is an integer. This is an error. Possibly you meant to convert the integer to a string, and concatenate the two strings, or you meant to convert the string to an integer and add the two integers together.
It appears that you are trying to concatenate, add, a string and an integer together. This is a no go. Try using the command, i = int(s), to convert your string, s, to an integer or use the commands = str(i) to convert your integer, i, to a string.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to separate a string by ':' sign and then select the left-hand side of this separated string.
These functions are working well and I got the right output.
df['title'].apply(lambda x: x.split(':')[0])
df['title'].str.split(':').str[0]
Why this code gives an error
df['title'].split(':')[0]
AttributeError: 'Series' object has no attribute 'split'
When you are using df["column"] you are getting a series object and the split() attributes is no applicable to that type of object. When using df["column"].str you are actually accessing the values of the series and python is identifying them as strings, so you can preform string functions on those objects.
You're trying to split the entire title column of the data frame (which is the Series mentioned in the error message). What you want to do is apply a function to each row of the data frame, which you can do by calling apply on the data frame, like you have done in your first code block.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This is my first week at college (Cyber security), and I don't have any coding background.
For a python assignment, I have to round the amounts to two decimal places, but if the last decimal is a zero, it should not show in the output. What is the appropriate rounding code I should use?
This is my code:
if (valuta == 1):
print("Voor", bedrag, "US Dollar krijgt u ", round(USDollar,2),"Euro, De transactiekosten bedragen", round(DollarTransactiekosten,2), "Euro. U ontvangt",round(AfgerondDollar,2), "Euro" )
Edit: Thanks for all the answers and help guys
It looks like you're new to programming, so here's a few pointers. I'm not going to write all the code you need for an answer here because then how will you learn? Instead, here's the thought process behind coming up with a way to code a solution to your problem.
We have a number x.
We want to round it to two decimal places.. Think about what format specifier you'll use. Let's save this result in str_x.
If the last character in this string is a zero, we want to get rid of it.
What we have now is what we required.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am trying to transform based on two lists using zip as follows. But i am getting 'int' object is not iterable. Help is appreciated. Here is the snippet. y and y_predicted are each lists of length 10.
errors = [sum((i-j)**2) for i,j in zip(y, y_predicted)]
(i-j)**2 is an integer. If you try to sum() an integer, you will receive the error.
You probably want to just sum the whole thing, right?
sum((i-j)**2 for i,j in zip(y,y_predicted))
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
For an online assignment to be submitted for a MOOC, I have written a python program that takes in text data (The name, roll numbers, grades, etc of a list of students) in a particular format and prints the output after processing the data. The output is printed from a list of tuples using a for loop to iterate through the list and using the str.join() function to join the elements in the tuples.
The list of tuples is sorted already and when I run it in the Spyder IDE console, it prints the output in the order that it appears in the list. But, when I submit in the window of the online judge and run it, the output is obtained in a random order and my answer doesn't get accepted. Can anyone please help? I am all ready to clarify further if my question isn't clear.
Thank you.
As #Epo rightly pointed out, I tested my input with alphabetically sorted data while the online judge used the same data sorted differently as input. Adding a "sorted()" function to the input did the job.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am reading in information from a text file which has been ripped from a pdf, so everything is a mess.
Some example variables (columns) that I'm trying to separate include date, action type and summary.
For date, the format is DD/MM/YY, so I know that the first index will always be an int. However, whenever I test the file (using type(xyz)), everything is marked as being an str.
How do I get python to recognize what is, and what is not, a str vs. int vs. double... etc.?
Short answer: use regular expressions and recast the string sections.
Long answer: it's because all of this is coming from a text file, so everything is a string. The date 23/10/90 isn't represented in a .txt as a numerical value, it's a collection of character codes. Depending on exactly what you are trying to get out of that file, your best bet is to regex out the data you want, and recast it. So, for dates, try int(dayString) int(monthString) etc.