I got a series name basepay that contains both String and Numeric element. What I wanted to do is to calculate the mean of the numeric part. I've tried basepay.mean() and the kernel return TypeError: unsupported operand type(s) for +: 'float' and 'str' So I tried to drop off the non-numeric part.
I used mask = basepay.astype(str).str.isnumeric() to create a mask. But all the elements in the returning series are False.
Shouldn't it return True when the element in the basepay is like '1234.32' ?
By the way, is there a faster way to deal with this problem ?
it might be easiest to just use a try catch block inside the mask function
like
try:
float(basepay)
catch:
do something if it fails
The .isnumeric() method doesn't accept decimal characters.
The documentation states:
Return True if all characters in the string are numeric characters,
and there is at least one character, False otherwise.
. is not clasified as a 'numeric character':
>>> '.'.isnumeric()
False
What you want is not .isalpha()
The documentation states:
Return True if all characters in the string are alphabetic and there is at least one character, False otherwise.
So not .isalpha() will check if there are any alphabetical characters and will return True if there are non. In this case . is not counted as an alphabetical character.
Example:
>>> '.'.isalpha()
False
Therefore:
>>> not '1234.32'.isalpha()
True
Related
I want to check a 'x' string whether it is a digit or not in advance.
'1' is naturally a digit.
But and I will use ① what is calld a string number very much.
I don't know the range of string numbers IDE judges as a digit.
'①'.isdigit() returns True.
'⑴'.isdigit() returns True.
'ⅰ' or 'Ⅰ' returns False.
'㈠' returns False. (kanji version of (1) )
'❶' returns True.
I want to do like this.
for s in data:
if s.isdigit():
int_ = int(s)
If I accept '①', int will throw an error. Now, I write try:except for it.
Because I'm a japanese, I often use '①' or '⑴'
How to distinguish isdigit or not isdigit in advance?
Should I rely on try:except or counting all of them in advance?
regular expression?
The main problem is I don't know what is judged as a digit.
data = ["1", "23", "345", "①", "(1)", "(2)"]
This data is dynamic value. It will be changed every time.
Moreover, the string like this may expand in the future.
I hope the string of isdigit() == True is accepted by int().
I don't have an urgent problem because of try: except.
I believe that the str.isdecimal method fits your requirements. It excludes strings like '①', but includes other strings like '١' which are accepted by int.
>>> int('١')
1
def is_valid_zip(zip_code):
"""Returns whether the input string is a valid (5 digit) zip code
"""
if (len(zip_code) == 5) and (str.isnumeric == True):
return True
else :
return False
First of all, it should be str.isnumeric() == True as that's calling the isnumeric function. Second of all you should be really using str.isdigit().
str.isnumeric()
In Python, decimal characters (like: 0, 1, 2..), digits (like: subscript, superscript), and characters having Unicode numeric value property (like: fraction, roman numerals, currency numerators) are all considered numeric characters. Therefore even japanese character for 1, 2 and 3 would pass this check.
str.isdigit()
On the other hand isdigit() will only return True if all characters in a string are digits. If not, it returns False.
source: https://www.programiz.com/python-programming/methods/string/isdigit
A few point to discuss. Regarding your condition:
str.isnumeric == True
That thing on the left side is the function itself, not a call to the function giving a result, the latter would be some_string.isnumeric().
The chances of the function object being equal to true are somewhere between zero and a very, very small number :-)
It's also redundant to compare boolean values against boolean constants since the result of the comparison is just another boolean value. Where do you stop in that case? For example:
(((some_bool_value == True) == True) == True) != False ...
Another point, the code form if cond then return true else return false can be replaced with the much less verbose return cond.
And also keep in mind that isnumeric() allows other things than raw digits, like ¾. If you just want the digits, you're probably better off with another method. You may be tempted to instead use isdigit(), but even that allows other things than just what most would consider "normal" digits, such as allowing "90²10" as a postal code, presumably the much trendier part of Beverly Hills :-).
If you only wanted the raw digits 0-9 (which is probably the case with US postal codes like you seem to be targeting), neither isnumeric() nor isdigit() is really suitable.
An implementation of the function, taking all that into account, could be as follows:
def is_valid_zip(zip_code):
if len(zip_code) != 5:
return False
return all([x in "1234567890" for x in zip_code])
it should be zip_code.isnumeric() not str.isnumeric
Also, why don't you use regex:
import re
RE_ZIP_CODE = re.compile(r'^[0-9]{5}$') # or r'^\d{5}$' for all digit characters
def is_valid_zip(zip_code):
return RE_ZIP_CODE.search(zip_code) is not None
This should work
def is_valid_zip(zip_code):
"""Returns whether the input string is a valid (5 digit) zip code
"""
if len(zip_code) == 5 and zip_code.isnumeric():
return True
else:
return False
print(is_valid_zip("98909"))
This question already has answers here:
How to check if a user input is a float
(6 answers)
Closed 4 years ago.
This code returns False, but when I delete point from float it becomes True and I'm trying to understand why. Explain please
def isDigit(string):
string = string.strip()
if string[:1] == "-":
cuted = string[1:]
if cuted.isdigit():
return True
else:
return False
elif string.isdigit():
return True
else:
return False
print isDigit("-234.4")
also I know my code is not the best and I wonder how can I make it better
isdigit only checks if all the characters are digits (e.g. 0, 1, ... 9).
Therefore the string 234.4 will return False because it also contains a decimal point ., that is not a digit.
help("".isdigit)
will tell you that to be True all characters must be digits and there must be at least one character.
You could use a regular expression to do this kind of checking, for example with something like:
import re
def isnumber(x):
return re.match("-?[0-9]+([.][0-9]+)?$", x) is not None
that will accept an optional minus sign, followed by a sequence of one or more digits optionally followed by a decimal dot and more digits.
Note that floating point numbers can be accepted by a much wider syntax, including scale exponent and missing parts before and after the decimal point so don't be this strict if you're validating output from a computer. Depending on the context it may be however meaningful to refuse things like 1e4 as numbers from a human.
Let's step through your code.
if string[:1] == "-":
string[:1] means "make a string with characters from the start of string up to (but not including) index 1." If your string is "foobar", string[:1] will be "f". In your example, string[:1] will be "-"
cuted = string[1:]
This will do the opposite, producing a string that contains everything but the first character. In this case, cuted would be "234.4"
if cuted.isdigit():
return True
else:
return False
This will test if cuted is made up only of numbers. In our case, this is false, because it contains a decimal point. False is returned.
elif string.isdigit():
return True
else:
return False
If the first character was not "-", this is run instead. If you supplied "234.4", this case would be reached, and the test would fail (because "234.4" contains a decimal point), so False would be returned.
Your code appears to be valid if what you wanted was:
123.3 -> False
-123.3 -> False
123 -> True
-123 -> True
On the other hand, if you want your function to say all four of those are numbers, then you need to modify your code. The one way (probably not the best!) to do that would be to have a test case that does something like this:
If I split this string on ".", do I get two strings?
If so, are both pieces of the string digits?
If so, the string is a number.
It's not clear exactly what behavior you want.
I'm trying to write a really simple function. It should return True if given object is a digit (0-9), False otherwise. Here are examples of input and desired output:
is_digit("") => False
is_digit("7") => True
is_digit(" ") => False
is_digit("a") => False
is_digit("a5") => False
My code works for the above examples.
def is_digit(n):
try:
return int(n) in range(0, 10)
except:
return False
Trouble is, the function returns True for n = "1\n" when it should return False. So, a string like "1" should be converted to integer and is a digit, but a string like "1\n" should not, yet I don't know how to get around that. How can I account for string literals?
P.S. If my title is lame, advice on renaming it is welcome.
You don't need to define a custom function for this. There is a built-in function for this, namely isdigit().
You can use it as: "a5".isdigit() or "1/n".isdigit().In both cases it will return False.
First you have to convert your literals into string then you can apply isdigit.
You can not apply isdigit directly to number. It will throw an error
AttributeError: 'int' object has no attribute 'isdigit'
You have to typecast your number in string.
eg:
In [3]: str(0).isdigit()
Out[3]: True
or
In [1]: "0".isdigit()
Out[1]: True
I feel like this is a simple question, but it keeps escaping me...
If I had a string, say, "1010101", how would I refer to the first digit in the string by its index?
You can get the first element of any sequence with [0]. Since a string is a sequence of characters, you're looking for s[0]:
>>> s = "1010101"
>>> s[0]
'1'
For a detailed explanation, refer to the Python tutorial on strings.
Negative indexes count from the right side.
digit = mystring[-1]
In Python, a sting is something called, subscriptable. That means that you can access the different parts using square brackets, just like you can with a list.
If you want to get the first character of the string, then you can simply use my_string[0].
If you need to get the last (character) in a string (the final 1 in the string you provided), then use my_string[-1].
If you originally have an int (or a long) and you are looking for the last digit, you are best off using % (modulous) (10101 % 10 => 1).
If you have a float, on the other hand, you are best of str(my_float)[-1]