Jun-11-2018, 12:33 PM
(Jun-11-2018, 11:50 AM)SriRajesh Wrote: I encounetr errr:
NameError Traceback (most recent call last)
<ipython-input-4-e50857fb9961> in <module>()
3 df2 = pd.DataFramedf = pd.DataFrame({'ids': ['AA-120amp', 'BA+250A-52amp', 'AA-5623amp','CD']})
4 df2 = df2.merge(df2['ids'].str.extractall(r'(?P<df2>[^-]+)-(?P<allele>(?P<rank>\d+).+)')
----> 5 .set_index(res.index.droplevel(1)),'inner', left_index=True, right_index=True)
6
NameError: name 'res' is not defined
OK, sorry - wrong merge of code. 3 strings
df2 = pd.DataFramedf = pd.DataFrame({'ids': ['AA-120amp', 'BA+250A-52amp', 'AA-5623amp','CD']}) extracted = df2['ids'].str.extractall(r'(?P<df2>[^-]+)-(?P<allele>(?P<rank>\d+).+)') df2 = df2.merge(extracted.set_index(extracted.index.droplevel(1)), 'inner', left_index=True, right_index=True)The result is
Output: ids df2 allele rank
0 AA-120amp AA 120amp 120
1 BA+250A-52amp BA+250A 52amp 52
2 AA-5623amp AA 5623amp 5623
Test everything in a Python shell (iPython, Azure Notebook, etc.)
- Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.