0

I am trying to read a column in python, and create a new column using python.

import pandas as pd 
df = pd.read_csv (r'C:\Users\User\Documents\Research\seqadv.csv') 
print (df)

df = pd.DataFrame(data={'WT_RESIDUE':['']})
codes = {'ALA':'A', 'ARG':'R', 'ASN':'N', 'ASP':'D', 'CYS':'C', 'GLU':'E', 'GLN':'Q', 'GLY':'G', 'HIS':'H', 'ILE':'I', 'LEU':'L', 'LYS':'K', 'MET':'M', 'PHE':'F', 'PRO':'P', 'SER':'S', 'THR':'T', 'TRP':'W', 'TYR':'Y', 'VAL':'V'}
df['MUTATION_CODE'] = df['WT_RESIDUE'].replace(codes)
df.to_csv (r'C:\Users\User\Documents\Research\output.csv')

I tried this, but it will not create a new column no matter what I do.

example

2
  • Can you please explain what column you are reading and what column you want to derive from it. Providing sample input and output will help. Please provide text and not images. Commented Apr 27, 2021 at 3:28
  • does the image answer the question? Commented Apr 27, 2021 at 3:28

2 Answers 2

0

It seems like you made a silly mistake

import pandas as pd 
df = pd.read_csv (r'C:\Users\User\Documents\Research\seqadv.csv') 
print (df)

df = pd.DataFrame(data={'WT_RESIDUE':['']}) # Why do you have this line?
codes = {'ALA':'A', 'ARG':'R', 'ASN':'N', 'ASP':'D', 'CYS':'C', 'GLU':'E', 'GLN':'Q', 'GLY':'G', 'HIS':'H', 'ILE':'I', 'LEU':'L', 'LYS':'K', 'MET':'M', 'PHE':'F', 'PRO':'P', 'SER':'S', 'THR':'T', 'TRP':'W', 'TYR':'Y', 'VAL':'V'}
df['MUTATION_CODE'] = df['WT_RESIDUE'].replace(codes)
df.to_csv (r'C:\Users\User\Documents\Research\output.csv')

Try removing the line with the comment. AFAIK, it is reinitializing your DataFrame and thus the WT_RESIDUE column becomes empty.

Sign up to request clarification or add additional context in comments.

Comments

0

Considering sample from provided input.
We can use map function to map the keys of dict to existing column and persist corresponding values in new column.

df = pd.DataFrame({
    'WT_RESIDUE':['ALA', "REMARK", 'VAL', "LYS"]
})
codes = {'ALA':'A', 'ARG':'R', 'ASN':'N', 'ASP':'D', 'CYS':'C', 'GLU':'E', 'GLN':'Q', 'GLY':'G', 'HIS':'H', 'ILE':'I', 'LEU':'L', 'LYS':'K', 'MET':'M', 'PHE':'F', 'PRO':'P', 'SER':'S', 'THR':'T', 'TRP':'W', 'TYR':'Y', 'VAL':'V'}
df['MUTATION_CODE'] = df.WT_RESIDUE.map(codes)

Input

    WT_RESIDUE
0   ALA
1   REMARK
2   VAL
3   LYS

Output

    WT_RESIDUE  MUTATION_CODE
0   ALA          A
1   REMARK       NaN
2   VAL          V
3   LYS          K

1 Comment

Using map is the same as replace. Prior to version 19.2, a difference in speed was pointed but it seems to be no longer the case. Have a look at this - stackoverflow.com/questions/42012339/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.