ben121:
私は、データフレームを持っています:
df = pd.DataFrame({'col1': [1,2,3,4,5], 'col2':[1,2,3,4,5], 'col3':['a','b','c','d','e']})
私は、col1とcol2には、それはノーだと思い、他の1に等しい場合は「はい」と言う新しい列を作成したいです。
私は、ラムダ関数が、無成功を使用して試してみました:
df['win'] = df[['col1', 'col2']].apply(lambda x: 'Yes' if (x['col1'] == 1) & (x['col2'] == 1) else 'No')
これを行うための良い方法はありますか?またはそれが動作するので、私がやっていることの改善。
HS-星雲:
はい、あなたは使用することができますnp.where
。
import pandas as pd
import numpy as np
df = pd.DataFrame({'col1': [1,2,3,4,5], 'col2':[1,2,3,4,5], 'col3':['a','b','c','d','e']})
condition = ((df['col1'] == 1) & (df['col2'] == 1))
# Return 'Yes' if the condition is True, and 'No' if False
df['win'] = np.where(condition, 'Yes', 'No')