By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,367 Members | 1,262 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,367 IT Pros & Developers. It's quick & easy.

Creating Multiple columns based on condition in a dataframe

P: 20
Hi There,

I am very new to python environment.
I am trying to replicate one of the SAS code in python.
I want to create 3 columns based on multiple conditions:

Below are the requirements:

if year is 2019 i want to create a column as en2019 and assign value as 1 else assign value as 0
same goes for year 2020.

Another condition is

if year=comm_year (2019=2019) then create a column 'commencing'and assign value as 'commencing' else assign value as 'Re-Enrolling'.

For your reference, I have attached the word document of SAS function and output which I am trying to replicate it in python.

Below is the data frame:

Expand|Select|Wrap|Line Numbers
  1. data = {'year': [2019, 2020, 2019, 2020, 2019],
  2.                'comm_year': [2019, 2019, 2020, 2020, 2019],
  3.        'count':[1,1,1,1,1]}
  4. enrol = pd.DataFrame(data, columns = ['year','comm_year','count'])
  5. enrol
Please advise on this.
Thanks.
Attached Files
File Type: docx condtions check.docx (33.7 KB, 56 views)
Feb 6 '20 #1
Share this Question
Share on Google+
2 Replies

100+
P: 200
To get the values ​​of multiple columns from pandas.DataFrame together, it can use the built-in function zip() for faster execution.
Expand|Select|Wrap|Line Numbers
  1. import pandas as pd
  2. data = {'year': [2019, 2020, 2019, 2020, 2019],
  3.         'comm_year': [2019, 2019, 2020, 2020, 2019],
  4.         'count':[1,1,1,1,1]}
  5. enrol = pd.DataFrame(data, columns = ['year','comm_year','count'])
  6. enrol
  7.  
  8. enrol_19=[]
  9. enrol_20=[]
  10. commencing=[]
  11.  
  12. for year, comm_year, count in zip(enrol['year'], enrol['comm_year'], enrol['count']):
  13.     if year == 2019:
  14.         enrol_19.append(count)
  15.     else:
  16.         enrol_19.append(0)
  17.     if year == 2020:
  18.         enrol_20.append(count)
  19.     else:
  20.         enrol_20.append(0)
  21.     if year == comm_year:
  22.         commencing.append('Commencing')
  23.     else:
  24.         commencing.append('Re_Enrolling')
  25.  
  26. enrol["enrol_19"]=enrol_19
  27. enrol["enrol_20"]=enrol_20
  28. enrol["Commencing"]=commencing
  29.  
  30. print(enrol)
Aug 26 '20 #2

100+
P: 305
What is the current output that you are getting?? Can you help us with that ??

Thanks
Aug 26 '20 #3

Post your reply

Sign in to post your reply or Sign up for a free account.