Let's assume my DataFrame df has a column called col of type string. What is wrong with the following code line?
df['col'].filter(str.isnumeric)
CodePudding user response:
You can do it like that:
df.loc[df['col'].str.isnumeric()]
CodePudding user response:
First problem, you're using a built-in python method without parenthesis which is str.isnumeric. Hence, the TypeError: 'method_descriptor' object is not iterable.
Second problem, let's suppose you've added parenthesis to str.isnumeric, this function needs one argument/string to check if all characters in the given string are numeric characters. Hence the TypeError: unbound method str.isnumeric() needs an argument.
Third problem, let's suppose you've fixed 1) and 2), since this function returns a boolean (True or False), you cannot pass it as a first parameter of pandas built-in method pandas.Series.filter. Hence, the TypeError: 'bool' object is not iterable.
As per the documentation, the first parameter needs to be a list-like :
items: list-like
Keep labels from axis which are in items.
In your case, I believe you want to use boolean indexing with pandas.DataFrame.loc :
import pandas as pd
df = pd.DataFrame({'col': ['foo', 'bar 123', '456']})
m = df['col'].str.isnumeric()
out = df.loc[m]
Output:
print(out)
col
2 456
