Getting all elements of type str in pd.DataFrame

Based on my little knowledge in pandas, I pandas.Series.str.containscan search for a specific string in pd.Series. But what if the volumetric frame is large, and I just want to look into it all kinds of str elements before doing anything?

Example:

pd.DataFrame({'x1':[1,2,3,'+'],'x2':[2,'a','c','this is']})
    x1  x2
0   1   2
1   2   a
2   3   c
3   +   this is

I need a function to return ['+','a','c','this is']

+4
source share
4 answers

If you look strictly at what are string values ​​and performance is not a concern, then this is a very simple answer.

df.where(df.applymap(type).eq(str)).stack().tolist()

['a', 'c', '+', 'this is']
+3
source

There are two possible ways - to check the numerical values ​​stored as strings or not.

Check the difference:

df = pd.DataFrame({'x1':[1,'2.78','3','+'],'x2':[2.8,'a','c','this is'], 'x3':[1,4,5,4]}) 
print (df)
     x1       x2  x3
0     1      2.8   1
1  2.78        a   4 <-2.78 is float saved as string
2     3        c   5 <-3 is int saved as string
3     +  this is   4

#flatten all values
ar = df.values.ravel()
#errors='coerce' parameter in pd.to_numeric return NaNs for non numeric
L = np.unique(ar[np.isnan(pd.to_numeric(ar, errors='coerce'))]).tolist()
print (L)
['+', 'a', 'c', 'this is']

, , float s:

def is_not_float_try(str):
    try:
        float(str)
        return False
    except ValueError:
        return True

s = df.stack()
L = s[s.apply(is_not_float_try)].unique().tolist()
print (L)
['a', 'c', '+', 'this is']

, , isinstance:

s = df.stack()
L = s[s.apply(lambda x: isinstance(x, str))].unique().tolist()
print (L)
['2.78', 'a', '3', 'c', '+', 'this is']
+3

You can use str.isdigitwithunstack

df[df.apply(lambda x : x.str.isdigit()).eq(0)].unstack().dropna().tolist()
Out[242]: ['+', 'a', 'c', 'this is']
+2
source

Using regular expressions and set union, you can try something like

>>> set.union(*[set(df[c][~df[c].str.findall('[^\d]+').isnull()].unique()) for c in df.columns])
{'+', 'a', 'c', 'this is'}

If you use a regex for the whole number , you can also omit floating point numbers.

+2
source

Source: https://habr.com/ru/post/1696002/


All Articles