python - 使用 pandas 使用列中的值格式化字符串

标签 python string pandas dataframe

使用 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 中?

最佳答案

mapstr.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/

相关文章:

python - 选项卡的使用不一致

python - 在 MySQL 中搜索短语关键字

c - 使用指针在c中反转字符串

python - pandas 多种条件下的新操作栏

python - 对 groupby 对象中的每个组应用重采样

python - 具有多个条件的 bool 索引

python - 将字符串转换为原始字节

python - 使用 pandas dataframes data python 创建堆叠直方图

sql - Oracle中将字符串拆分为多行

java - 使连接字符串识别为 java 中声明的变量