1

Using Python 3, I'm trying to replace a certain word in a URL that I've put into a Dataframe with 732 rows of the same URL. This is the URL: http://dbarchive.biosciencedbc.jp/kyushu-u/hg19/eachData/bed20/**ID**.bed.

I have another Dataframe that contains 732 rows of different experimental IDs. I want to be able to replace the word 'ID' in the URL with each of the experimental IDs so that I get an updated Dataframe with every single one of the 732 URLs I need to download a .bed file into Python.

As a side note - from there, is it possible to download a .bed file into Python without having to first save it through my browser and then upload it into Python?

0

2 Answers 2

1

Use map with str.format.

import random

# Setup
url = 'http://.../bed20/{}.bed' 

np.random.seed(0)
df = pd.DataFrame({'ID': np.random.choice(100, 5).astype(str)})   

df['ID'].map(url.format)

0    http://.../bed20/44.bed
1    http://.../bed20/47.bed
2    http://.../bed20/64.bed
3    http://.../bed20/67.bed
4    http://.../bed20/67.bed
Name: ID, dtype: object

Replace with your own URL and ID dataframes.


Alternatively, use a list comprehension (should be around the same in terms of performance) ...

[url.format(x) for x in df['ID']]    
# ['http://.../bed20/44.bed', 
#  'http://.../bed20/47.bed', 
#  'http://.../bed20/64.bed', 
#  'http://.../bed20/67.bed', 
#  'http://.../bed20/67.bed']

df.assign(ID=[url.format(x) for x in df['ID']])

                        ID
0  http://.../bed20/44.bed
1  http://.../bed20/47.bed
2  http://.../bed20/64.bed
3  http://.../bed20/67.bed
4  http://.../bed20/67.bed
Sign up to request clarification or add additional context in comments.

2 Comments

The actual experimental ID is made up of numbers and letters and can't be randomized. How do I do that?
@Lauren It works the same way no matter what IDs you have. I can't solve your problem for you because I don't have your data. But I've shown you how you can solve your problem.
0

I'd use apply and format

fmt = 'http://dbarchive.biosciencedbc.jp/kyushu-u/hg19/eachData/bed20/{}.bed'
df.ID.apply(fmt.format)

1 Comment

How do I insert the IDs from my other dataframe though? Keeping in mind that there's 732 unique IDs so I have to create 732 unique URLs?

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.