Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make a new column with a specific condition?

I want to make a new column with this condition:

  • If the the value on Case Number column in current row equals with the previous row, then the value should be taken from column ‘diff’
  • If the current row is not equal with the previous row, then the value should be taken from ‘lastmod-start’ column.

This is the code that I’ve tried :

df['new'] = [None] * len(df)
for i in range(1,len(df)):
    if df['Case Number'][i] == df['Case Number'][i-1]:
        df['new'][i] = df['diff']
    else:
        df['new'][i] = df['lastmod-start']

However the code above is resulting an error.
Is the anyone can help me? Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is the screenshot The result

>Solution :

First, you can assign a new column as a single value and pandas will broadcast it to the entire column. Since the 'new' column will be string, you can just initialize it to an empty string.

df['new'] = ''

Next, if you want to compare each row to the row before it, you can use the .shift() method create a boolean index of which rows match. Then use that index to assign the values.

ix = df['Case Number'] == df['Case Number'].shift()
df.loc[ix, 'new'] = 'diff'
df.loc[~ix, 'new'] = 'lastmod-start'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading