Check string presence in multiple cols and give output to separate cols in pandas

Viewed 48

I have dataframe as like follows:

    col1  col2  col3  col4
0   a-I    -     -    12.4
1   -      a-I   -    2.6
2   a-I    -     a-I  23
3   -      a-I   a-I  4
4   -      -     a-I  5
5   a-I    a-I   -    6.245
6   -      a     a    7.23
7   a      a     -    8

What I want is to check for I in each column if it is present than make res column with splitting string with -. Can anybody have any idea how to do this?

Expected Result would be like this:

    col1  col2  col3  col4 res
0   a-I    -     -    12.4  I 
1   -      a-I   -    2.6   I
2   a-I    -     a-I  23    I
3   -      a-I   a-I  4     I
4   -      -     a-I  5     I
5   a-I    a-I   -    6.245 I
6   -      a     a    7.23  -
7   a      a     -    8     -
2 Answers

Setup

If you have other datatypes besides str in your DataFrame, you will need to replace all mentions of df with df.astype(str).

Using numpy.where and str.contains

df.assign(res=np.where(df.sum(1).str.contains('I'), 'I', '-'))

Using a list comprehension:

List comprehensions are often faster than builtin pandas string methods:

df.assign(res=['I' if 'I' in row else '-' for row in df.values.sum(1)])

Both produce:

 col1 col2 col3 res
0  a-I    -    -   I
1    -  a-I    -   I
2  a-I    -  a-I   I
3    -  a-I  a-I   I
4    -    -  a-I   I
5  a-I  a-I    -   I
6    -    a    a   -
7    a    a    -   -

Timings

df = pd.concat([df]*5000)

%timeit ['I' if 'I' in row else '-' for row in df.values.sum(1)]
9.29 ms ± 310 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit np.where(df.sum(1).str.contains('I'), 'I', '-')
69.4 ms ± 2.17 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

%timeit df.sum(1).str.contains('I').map({False:'-',True:'I'})
83 ms ± 12.5 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

OR:

df['res']=df.sum(1).str.contains('I').map({False:'-',True:'I'})
print(df)

If it the pandas data frame contains types other than strings, do:

df['res']=df.astype(str).sum(1).str.contains('I').map({False:'-',True:'I'})
print(df)
Related