使用 Python 3,我尝试替换已放入 Dataframe 中的 URL 中的某个单词,其中包含 732 行相同的 URL。这是网址:http://dbarchive.biosciencedbc.jp/kyushu-u/hg19/eachData/bed20/**ID**.bed
。
我有另一个 Dataframe,其中包含 732 行不同的实验 ID。我希望能够用每个实验 ID 替换 URL 中的“ID”一词,这样我就可以获得一个更新的 Dataframe,其中包含将 .bed 文件下载到 Python 中所需的 732 个 URL 中的每一个。
顺便说一句 - 从这里开始,是否可以将 .bed 文件下载到 Python 中,而不必先通过浏览器保存它,然后将其上传到 Python 中?
最佳答案
将map
与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
替换为您自己的 URL 和 ID 数据框。
<小时/>或者,使用列表理解(在性能方面应该大致相同)...
[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
关于python - 使用 pandas 使用列中的值格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45203472/