-3

I have a data set like this

ID    Status
1     Completed
2     Completed
3     Cancelled
4     Cancelled
5     Not Available
6     Not Available

I want to create new column and put value 1 wherever status got completed

ID    Status          Supply
1     Completed         1
2     Completed         1 
3     Cancelled
4     Cancelled
5     Not Available
6     Not Available

Thanks

JamCon
  • 2,313
  • 2
  • 25
  • 34
Aaron
  • 1
  • 2
  • 2
    Welcome to StackOverflow. This question is missing context or other details: Please improve the question by providing additional context, which ideally includes your thoughts on the problem and any attempts you have made to solve it. This information helps others identify where you have difficulties and helps them write answers appropriate to your experience level. You also need to state exactly what your difficulty is, what you expected, what you got, and any traceback. – Rory Daulton Mar 02 '19 at 17:21
  • Possible duplicate of [Pandas conditional creation of a series/dataframe column](https://stackoverflow.com/questions/19913659/pandas-conditional-creation-of-a-series-dataframe-column) –  Mar 02 '19 at 17:31

2 Answers2

0

If df is your DataFrame

           Status
ID               
0       Completed
1       Completed
2       Cancelled
3       Cancelled
4   Not available
5   Not available

Use df["Status"] == "Completed" to find the rows where condition is fulfilled and then convert the boolean to integer using .astype(int).

df["Supply"] = (df["Status"] == "Completed").astype(int)

The result is

          Status     Supply
ID                       
0       Completed       1
1       Completed       1
2       Cancelled       0
3       Cancelled       0
4   Not available       0
5   Not available       0
Julia
  • 397
  • 3
  • 14
0

Please explain your question carefully and use code sample format to write codes. From what i understood this is what you want:

import pandas as pd
import numpy as np
#Existing Data
data={'id':[1,2,3,4,5,6],'status':['Completed','Completed','Cancelled','Cancelled','Na','Na']}
#Create a dataframe
df=pd.DataFrame(data)
#Add the 'Supply' Column
df['supply']=np.where(df['status']=='Completed',1,0)

Check this for more info: Pandas Conditional creation of DataFrame

Also please post the code of what you tried.

Good Luck!

May the Code be with you!

  • 1
    Welcome to Stack Overflow! Please stop answering questions that you yourself know are bad. If you have to tell the asker that their question is unclear, if you have to tell them to show what they tried to solve it, if you know it's a duplicate: don't answer. Leave a comment. If you can't comment yet: leave answers on _good_ questions which are worth your time. See also [the help center](https://stackoverflow.com/help/how-to-answer). – Andras Deak -- Слава Україні Mar 03 '19 at 11:33
  • 1
    okay from next time! –  Mar 03 '19 at 14:43