473,473 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Creating Multiple columns based on condition in a dataframe

20 New Member
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, 106 views)
Feb 6 '20 #1
2 4031
SioSio
272 Contributor
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
madankarmukta
308 Contributor
What is the current output that you are getting?? Can you help us with that ??

Thanks
Aug 26 '20 #3

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

Similar topics

6
by: Dennis | last post by:
In CSS3 it looks like we'll have multiple column flowing of text (newspaper style) in which the number of columns can be determined automatically given the available horizontal space....
16
by: lostinspace | last post by:
Is it possible? TIA
7
by: Billy Jacobs | last post by:
I am using a datagrid to display some data. I need to create 2 header rows for this grid with columns of varying spans. In html it would be the following. <Table> <tr> <td colspan=8>Official...
1
by: Hetal | last post by:
Hi... We are on Visual Basic .NET 2003 and i am using DataView to sort the DataTable. However, we are facing problems with sorting the data based on multiple columns. The below statement to...
2
by: nico3334 | last post by:
Hi, I have a SQL table that has data like this: Title Month Info 1 ---- 7 ---- 100 1 ---- 7 ---- 100 1 ---- 8 ...
3
by: Vinda | last post by:
Hi Bytes, Using a previous question as a base Access 2000 Inserting multiple rows based on a date range. I also wanted to insert multiple rows into a table according to a date range supplied by a...
0
by: bbaamm | last post by:
he database result is as follows DATE code class1 class2 class3 1/8/10 M1 10 20 30 3/8/10 V2 20 30 10 1/8/10 H1 1 2 3 ...
1
by: kffacs | last post by:
Multiple rows based on a date range I have an MSAccess 2007 DB to record our employees Personal Days Off (PDO). Until now I have only had a form to record each single day taken. This results in...
16
by: fyamoahaxracer | last post by:
Hi everyone, I have spent a few hours searching and cannot find an example that is similar enough for me to work with. Is it possible to combine multiple rows by creating new columns? Searching...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.