Andrea Ianni
Andrea Ianni

Reputation: 839

Pandas: if-else not working on a column

I want to define a column in which I want to put 1 or 0 if the date contained in another column is equal to a certain data.

DF[['column_i']]= 0 if DF[['column_j']] == '1983-11-08' else 1

It seems that the operation is not defined univocally because it returns an error:

Traceback (most recent call last):   File "<pyshell#150>", line 1, in
<module>
    Accounts_conFatt_SR_TRAFF[['Churn']]= 0 if Accounts_conFatt_SR_TRAFF[['Deactive Date']] == '1970-01-01' else 1  
File "C:\Program
Files\Anaconda\lib\site-packages\pandas\core\generic.py", line 887, in
__nonzero__
    .format(self.__class__.__name__)) ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or
a.all().

Is it a bug or am I doing something wrong?

ps= no problems with the types: I have already tried to perform the if on a single DF['column'][0] value and it works well.

Upvotes: 0

Views: 500

Answers (2)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

in this particular case (when you need zero or one) you can do it this way:

DF[['column_i']] = (DF[['column_j']] != '1983-11-08').astype(np.int8)

using the fact that True and False values are represented as 1 and 0 internally in Python.

PS but the solution from @reptilicus is more general as you can put there any values not only zero or one.

Upvotes: 1

reptilicus
reptilicus

Reputation: 10397

You can do something like this using a numpy.where statement:

df['column_i'] = np.where(df['column_j'] == '1983-11-08', 0, 1)

Upvotes: 2

Related Questions