Gene Renaming Columns Dataframe
I want to extract a character before and after certain characters in a string, most of these are in a pandas dataframe column.
Basically I want to take from my principal dataframe and merge together is from my columns 'Strain' and 'Region' taking the following items:
i) Original Strain: Streptomyces_sp_QL40_O
ii) Original Region: Region 1.1
Extract:
- The string after the second underscore Ex: QL40
- The first number before the '.' Ex: nbsp.1
- The second number after the '.' Ex: .1
- The string region before the '&' character
- Finally to add two 0's after string 'region' if digit is less than 10 and one 0 if digit is more than ten.
Desired Output: QL40_1.region001
I am almost there but can't place the 0's after the region and before the last number.
Here is a snippet of my code if anyone has any idea on what I might be doing incorrectly?
def modify_dataframe(df_rows):
regions = []
for i in df_rows['Region'].str.split('.').str[0]:
regions.append(''.join([d for d in i if d.isdigit()]))
df_rows['BGC Region'] = df_rows['Strain'].str.split('_').str[2] + '_' + regions + '.region'
print (df_rows)
region_number = df_rows['Region'].str.split('.').str[1]
for i, rn in enumerate(region_number):
if int(rn) < 10:
df_rows['BGC Region'][i] += '00' + rn
elif int(rn) < 100:
df_rows['BGC Region'][i] += '0' + rn
return df_rows
• 332 views
•
link
0 answers
No answers yet.
Log in to answer this question.
Hello biohacker_tobe!
We believe that this post does not fit the main topic of this site.
This is a highly specific programming problem that does not involve any broad bioinformatics concepts. It is not helpful to the community at large as it is asking us to basically debug your custom code for you.
For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.
If you disagree please tell us why in a reply below, we'll be happy to talk about it.
Cheers!
Fair enough, I understand. Maybe this was a bit too specific