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

set value to new column according to specific index

I want to create new column name 'pred_date' in df and set value according to specific index.

For example for index [1, 2, 3] I want to set value as the value in date column.

df:

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

    date   label    
1   1.1     1  
2   2.1     0
3   3.1     1 
4   4.1     1

new df:

    date   label    pred_date    
1   1.1     1       1.1 
2   2.1     0       2.1
3   3.1     1       3.1
4   4.1     1       Nan

>Solution :

You could use index.isin + where:

df['pred_date'] = df['date'].where(df.index.isin([1,2,3]))

Output:

   date  label  pred_date
1   1.1      1        1.1
2   2.1      0        2.1
3   3.1      1        3.1
4   4.1      1        NaN
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