coder7
coder7

Reputation: 11

how can I copy value from one column to another in dataframe conditionally?

Dataframe structure:

df = pd.DataFrame([[1,2,3], ['Active','Deleted','Active'], [np.nan,2,np.nan]], 
                  columns=list('ABC'))

If value of column B is 'Deleted' then I want to copy value for column A into column C else keep it Nan. How can I do it in pandas?

Upvotes: 0

Views: 57

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

You want where:

df['c'] = df['a'].where(df['b']=='A')

Upvotes: 1

Related Questions