I'm a beginner at pandas (I apologize if i'm using the wrong terminology) and i am currently working on a genomics project. I'm having trouble manipulating dataframes columns after using drop_duplicates(). I want to change the column values in the column 'mutation' of the id that is kept after dropping duplicates to indicate that this id has multiple mutations.
df = pd.DataFrame([
('MYC', 'nonsense', 's1'),
('MYC', 'missense', 's1'),
('MYCL', 'nonsense', 's1'),
('MYCL', 'missense', 's2'),
('MYCN', 'missense', 's3'),
('MYCN', 'UTR', 's1'),
('MYCN', 'nonsense', 's1')
], columns=['id', 'mutation', 'sample'])
print(df)
id mutation sample
0 MYC nonsense s1
1 MYC nonsense s1
2 MYC missense s1
3 MYCL nonsense s1
4 MYCL missense s2
5 MYCN missense s3
6 MYCN UTR s1
7 MYCN nonsense s1
I tried using drop_duplicates() and i am getting close to what i want. But how do i change the value in the column 'mutation' to 'multi'?
print(df.drop_duplicates(subset=('sample','id')))
id mutation sample
0 MYC nonsense s1
3 MYCL nonsense s1
4 MYCL missense s2
5 MYCN missense s3
6 MYCN UTR s1
id mutation sample
0 MYC multi s1
3 MYCL nonsense s1
4 MYCL missense s2
5 MYCN missense s3
6 MYCN multi s1
duplicated
mask = df.duplicated(['id', 'sample'], keep=False)
df.assign(mutation=df.mutation.mask(mask, 'multi')).drop_duplicates()
id mutation sample
0 MYC multi s1
2 MYCL nonsens s1
3 MYCL missense s2
4 MYCN missense s3
5 MYCN multi s1
groupby
df.groupby(['id', 'sample'], sort=False).mutation.pipe(
lambda g: g.first().mask(g.size() > 1, 'multi')
).reset_index().reindex(df.columns, axis=1)
id mutation sample
0 MYC multi s1
1 MYCL nonsens s1
2 MYCL missense s2
3 MYCN missense s3
4 MYCN multi s1
df.loc[df.duplicated(subset=['id', 'sample'], keep='last'), 'mutation'] = 'multi'
df.drop_duplicates(subset=['id', 'sample'])
Explanation: first identify which are the duplicates and change the mutation column for those. Only afterwards, drop the duplicates.
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.