New Column If condition

Hi guys I am new to python and I need some solution or hint what alternative can be used . I am trying to create a new column based on existing column of python for some 77k records.

I already tried a solution but this below piece of code is taking 25 mins to do so.

can anyone please help me how to do in some 1 or 2 mins. I do same step in SAS in 30 secs. i need in python

def dataset(df row):

if df row[‘Past_ID’] in (‘AAA’, ’ ‘):
df_row[ ‘Current_ID’] = "TEST’

Else
df_row[‘Current_ID’] = df_row[‘Past_ID’]

if df row[‘Past_ID’] in (BBB’, ’ ‘):
df_row[ ‘Current_ID’] = "TEST’

if df row[‘Past_ID’] in (CCC’, ’ ‘):
df_row[ ‘Current_ID’] = "TEST’
return df_row

dataset = dataset, apply(testifconn, axis=1)
dataset

Hi Shiv and welcome!

Sorry but I don’t understand your code. If you are using the web
interface to post, please format the code as code, to keep the
formatting. If you are posting from email, please put four spaces before
each line of code.

You start by defining a function called “dataset”:

def dataset(df row):

but that’s not legal Python, because variables cannot include a space.
So I don’t know what “df row” is supposed to mean.

def dataset(df, row):
def dataset(df_row):

Maybe the underscore?

    if df row['Past_ID'] in ('AAA', ' '):
        df_row[ 'Current_ID'] = "TEST'
    Else
        df_row['Current_ID'] = df_row['Past_ID']

Again, not legal Python, Python is case-sensitive. If you changed
else: (legal) to Else (not legal), what other bugs have you inserted
into the code you show us?

If you want us to diagnose a problem with code, you have to show us the
actual code you are using, not something “nearly the same, except it’s
broken and bits are missing and it won’t work because it’s full of
typos, but you can guess what it’s supposed to be, right?”

:slight_smile:

You then have:

dataset = dataset, apply(testifconn, axis=1)
dataset

but I can’t work out what that is supposed to be. dataset is a
function; then you assign it to a tuple of the same function, with the
mystery “apply” function called on the even more mysterious
“testifconn”.

My wild guess is that you ought to look at this “testifconn” function.
What does it do and how slow is it?

Hello Steven,

Thank for the quick reply. I’m sorry that this is not my actual code it’s just a replica I typed here with few changes of columns and values may be with few typo. This current code is working accurately for results but taking longer time.

Can you help if I can use any other function or program which fullfill my requirement as this code is running for 25 mins.

My requirement is just to create a new column based on existing column values.

More than a few, I’m afraid. Seriously, this will go a lot faster if you give us actual (if cut-down) runnable code, formatted so that we can read it. Without that, all we can do is guess.

Hi james below would be similar code to mine . But this if conditions statement taking 25 min to apply 75k records . I also tried to apply cprofile till it’s taking same time . Could you tell me any alternate way

def SCHOOL (df_row) :

    if df_row['STUDENT_GRADE') in ('AB',''):
               df_row["AVERAGE_GRADE'] = 'A'
    else:
              df_row['AVERAGE_GRADE']
                      = df_row['STUDENT_GRADE']
    if df_row['STUDENT_GRADE') in ('AC',''):
              df row[' AVERAGE_GRADE'] = 'A'
    if df_row['STUDENT_GRADE') in ('AD',''):
              df row['AVERAGE_GRADE'] = 'A' 
    if df_row['STUDENT_GRADE'] in ('AE',''):
              df row['AVERAGE_GRADE'] = 'A'
    return df_row

GRADE_Details = SCHOOL1.apply(SCHOOL, axis=1) GRADE Details

This is still not runnable code.

I’ve offered you a suggestion in your other post on this same subject. That’s a bit rude, by the way, posting a second copy of your request.

Hi James I’m sorry I don’t mean to do but I thought the other query is not exactly correct so re-created the query and posted again as new.

Both of them are broken in exactly the same way, so it didn’t help. Next time, please copy and paste the exact code you use; if you retype something, it is almost inevitable that you will make mistakes.