user12267139
user12267139

Reputation:

keep only strings in a dataframe column

In this df I want only to keep the strings in each row and deleate everything else

     Values          
0   99;3;;Sicherheitstür (0SS4.2) bei Anfang Boxen...
1   100;3;;Sicherheitstür (0SS4.2) bei Anfang Boxe...
3   145;3;;Sicherheitstür (0SS3b.5) bei Einspeisef...
4   95;3;;Sicherheitstür (0SS3b.5) vor Boxen unten...
5   96;3;;Sicherheitstür (0SS3b.5) vor Boxen unten...
6   30;3;;Anlage ausgeschaltet (Schlüsselschalter ...
7   37;3;;Sicherheitsbereich 5 (Paketierung) ausge...

I found this answer here

but got a Syntax error


  File "<ipython-input-17-a2c397c4c493>", line 1
    df = df[df['Value'].apply(lambda x: isinstance(x, basestring)]
                                                             ^
SyntaxError: invalid syntax

Upvotes: 0

Views: 730

Answers (1)

jezrael
jezrael

Reputation: 862481

In python 3 use str instead basestring:

 df = df[df['Value'].apply(lambda x: isinstance(x, str))]

If want first extract all strings values and then count:

s = df['Values'].str.extractall('([a-zA-Z]+)')[0].value_counts()
print (s)
SS                    5
Sicherheitstur        5
b                     3
bei                   3
Boxen                 3
unten                 2
Anfang                2
vor                   2
ausge                 1
Boxe                  1
Anlage                1
Sicherheitsbereich    1
Einspeisef            1
ausgeschaltet         1
Paketierung           1
Schlusselschalter     1
Name: 0, dtype: int64

Upvotes: 1

Related Questions