は、単に、その後others
としてそれらの残りの部分を設定するためにnp.where
で使用することができブールマスクを作成するためにnp.in1d
でapple
かmango
ているアイテムを探します。したがって、我々は持っているだろう - 例については
df['b'] = np.where(np.in1d(df.a,['apple','orange']),df.a,'others')
を使用すると、大きな文字列の一部としてこれらの名前を持つ文字列で動作するように見ている可能性がある場合、あなたは@jezrael's solution
からこのアイデアをキャッチ(str.extract
を使用することができ、私はそれは大丈夫です願っています! )、その後、np.where
を使用し、そのような -
strings = df.a.str.extract('(orange|apple)')
df['b'] = np.where(np.in1d(strings,['apple','orange']),strings,'others')
サンプル実行 -
In [294]: df
Out[294]:
a
0 apple-shake
1 orange
2 apple-juice
3 apple
4 mango
5 orange
6 banana
In [295]: strings = df.a.str.extract('(orange|apple)')
In [296]: df['b'] = np.where(np.in1d(strings,['apple','orange']),strings,'others')
In [297]: df
Out[297]:
a b
0 apple-shake apple
1 orange orange
2 apple-juice apple
3 apple apple
4 mango others
5 orange orange
6 banana others
私は結果は3つの可能性の一つになりたい: 'りんご'、 'オレンジ' や「O thers '。 – nos
私は答えを編集して、それを確認してください。 – jezrael