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

Creating Multiple columns based on condition in a dataframe

P: 7
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, 23 views)
2 Weeks Ago #1
Share this question for a faster answer!
Share on Google+

Post your reply

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