简体   繁体   中英

pandas dataframe create a new binary activity column based on another file containg a column of names

                  LigandName    Score
0   _017_model1/_017_model1     -10.54
1   _010_model1/_010_model1     -9.49
2   _113_model1/_113_model1     -9.40
3   _009_model1/_009_model1     -9.37
4   _193_model1/_193_model1     -9.36

This is the first few rows of the dataframe. I want to add a new column called IsActive, which is based on the presence of the LigandName in that file. This is the file

0   _017_model1/_017_model1
1   _010_model1/_010_model1
2   _113_model1/_113_model1
3   _009_model1/_009_model1
4   _004_model1/_004_model1

If the LigandName is present, the IsActive entry should be 1, and 0 if it wasn't. What should I do?

Create boolean mask by Series.isin and cast to integers - True s are 1 and False s are 0 :

print (df1)
                LigandName
0  _017_model1/_017_model1
1  _010_model1/_010_model1
2  _113_model1/_113_model1
3  _009_model1/_009_model1
4  _004_model1/_004_model1

df['IsActive'] = df['LigandName'].isin(df1['LigandName']).astype(int)
print (df)
                LigandName  Score  IsActive
0  _017_model1/_017_model1 -10.54         1
1  _010_model1/_010_model1  -9.49         1
2  _113_model1/_113_model1  -9.40         1
3  _009_model1/_009_model1  -9.37         1
4  _193_model1/_193_model1  -9.36         0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM