RJF
RJF

Reputation: 447

Pandas: Select rows containing only strings?

I have a data-frame that looks like this:

    [Column1]   [Column2]
0   16155.22300 1.246982
1   16193.009   BMS1P17,BMS1P18,BMS1P22,DUXAP8
2   16231.289   LINC01297
5   16265.05300 2.156268
6   16287.937   POTEH,POTEH-AS1
7   16288.53800 2.156268
10  17645.92500 44.765792
11  17646.335   HDHD5,HDHD5-AS1
12  17646.44400 44.765792
15  18073.59200 103.154877
16  18073.656   LOC101929372,SLC25A18
17  18073.84300 103.154877

I'd like to make list containing only strings from the column2. My solution to this is something like this:

my_list=list(i for i in ndf['LDU'] if isinstance(i, basestring))

For some reason it is not working and it returns all values. Any suggestion is much appreciated!

Upvotes: 2

Views: 1586

Answers (3)

Biarys
Biarys

Reputation: 1173

Here's a quick solution:

import pandas as pd
df = pd.DataFrame({"a":[2,3,4], "b":["string",2,'m']})

my_list = []
for index, row in df["b"].iteritems(): #change b to your col name
    if type(row) == str:
        my_list.append(row)
print(my_list)

outputs: ['string', 'm']

Upvotes: 2

harpan
harpan

Reputation: 8631

You can use:

df['Column2'].loc[pd.to_numeric(df['Column2'], errors='coerce').isnull()]

Or if you want it in a list.

list(df['Column2'].loc[pd.to_numeric(df['Column2'], errors='coerce').isnull()])

Upvotes: 6

zipa
zipa

Reputation: 27879

You can use this:

def checker(txt):
    try:
        float(txt)
        return False
    except:
        return True

df[df['[Column2]'].apply(checker)]
#    [Column1]                       [Column2]
#1   16193.009  BMS1P17,BMS1P18,BMS1P22,DUXAP8
#2   16231.289                       LINC01297
#6   16287.937                 POTEH,POTEH-AS1
#11  17646.335                 HDHD5,HDHD5-AS1
#16  18073.656           LOC101929372,SLC25A18

Upvotes: 4

Related Questions