如何使用 dataframe/series 个值创建 URL 个字符串的列表?
How do I create list of URL strings using a dataframe/series of values?
我有一系列值 IDs
,我将其转换为数据帧 dfA
,输出为:
IDs
0
ID_3456789065
1
ID_4546567657
2
ID_1765768878
我正在尝试将 dfA['IDs']
转换为 URL 的字符串列表,输出为:
'https://api.names.io/v1/ids/ID_3456789065/IDAccounts'
'https://api.names.io/v1/ids/ID_4546567657/IDAccounts'
'https://api.names.io/v1/ids/ID_1765768878/IDAccounts'
如果我手动输入 ID,这会得到我想要的:
acctID = 'ID_3456789065'
f'https://api.names.io/v1/ids/{acctID}/IDAccounts'
输出:
'https://api.names.io/v1/ids/ID_3456789065/IDAccounts'
当我尝试使用不同版本的 dfA
循环时,我总是得到错误的输出或错误:
urlList=[]
for i in dfA:
acctID = dfA[i]
urlList = f'https://api.names.io/v1/ids/{acctID}/IDAccounts'
urlList.append(urlList)
输出:
https://api.names.io/v1/ids/0 ID_3456789065
1 ID_4546567657
2 ID_1765768878
Name: data_bettor, dtype: object/IDAccounts
我也尝试过 .concat 并抛出 TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid
请帮忙!
您可以使用遍历“ID”列的列表理解:
out = [f'https://api.names.io/v1/ids/{acctID}/IDAccounts' for acctID in df['IDs']]
输出:
['https://api.names.io/v1/ids/ID_3456789065/IDAccounts',
'https://api.names.io/v1/ids/ID_4546567657/IDAccounts',
'https://api.names.io/v1/ids/ID_1765768878/IDAccounts']
您可以使用:
urlList = list('https://api.names.io/v1/ids/' + df['IDs'] + '/IDAccounts')
print(urlList)
# Output
['https://api.names.io/v1/ids/ID_3456789065/IDAccounts',
'https://api.names.io/v1/ids/ID_4546567657/IDAccounts',
'https://api.names.io/v1/ids/ID_1765768878/IDAccounts']
您可以使用 apply()
on the IDs
column and use to_list
从系列中获取列表:
df["IDs"].apply(lambda acctID: f'https://api.names.io/v1/ids/{acctID}/IDAccounts').to_list()
这输出:
['https://api.names.io/v1/ids/ID_3456789065/IDAccounts',
'https://api.names.io/v1/ids/ID_4546567657/IDAccounts',
'https://api.names.io/v1/ids/ID_1765768878/IDAccounts']
我有一系列值 IDs
,我将其转换为数据帧 dfA
,输出为:
IDs | |
---|---|
0 | ID_3456789065 |
1 | ID_4546567657 |
2 | ID_1765768878 |
我正在尝试将 dfA['IDs']
转换为 URL 的字符串列表,输出为:
'https://api.names.io/v1/ids/ID_3456789065/IDAccounts'
'https://api.names.io/v1/ids/ID_4546567657/IDAccounts'
'https://api.names.io/v1/ids/ID_1765768878/IDAccounts'
如果我手动输入 ID,这会得到我想要的:
acctID = 'ID_3456789065'
f'https://api.names.io/v1/ids/{acctID}/IDAccounts'
输出:
'https://api.names.io/v1/ids/ID_3456789065/IDAccounts'
当我尝试使用不同版本的 dfA
循环时,我总是得到错误的输出或错误:
urlList=[]
for i in dfA:
acctID = dfA[i]
urlList = f'https://api.names.io/v1/ids/{acctID}/IDAccounts'
urlList.append(urlList)
输出:
https://api.names.io/v1/ids/0 ID_3456789065
1 ID_4546567657
2 ID_1765768878
Name: data_bettor, dtype: object/IDAccounts
我也尝试过 .concat 并抛出 TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid
请帮忙!
您可以使用遍历“ID”列的列表理解:
out = [f'https://api.names.io/v1/ids/{acctID}/IDAccounts' for acctID in df['IDs']]
输出:
['https://api.names.io/v1/ids/ID_3456789065/IDAccounts',
'https://api.names.io/v1/ids/ID_4546567657/IDAccounts',
'https://api.names.io/v1/ids/ID_1765768878/IDAccounts']
您可以使用:
urlList = list('https://api.names.io/v1/ids/' + df['IDs'] + '/IDAccounts')
print(urlList)
# Output
['https://api.names.io/v1/ids/ID_3456789065/IDAccounts',
'https://api.names.io/v1/ids/ID_4546567657/IDAccounts',
'https://api.names.io/v1/ids/ID_1765768878/IDAccounts']
您可以使用 apply()
on the IDs
column and use to_list
从系列中获取列表:
df["IDs"].apply(lambda acctID: f'https://api.names.io/v1/ids/{acctID}/IDAccounts').to_list()
这输出:
['https://api.names.io/v1/ids/ID_3456789065/IDAccounts',
'https://api.names.io/v1/ids/ID_4546567657/IDAccounts',
'https://api.names.io/v1/ids/ID_1765768878/IDAccounts']