英文:
How can I remove a column in a pandas DataFrame and make another column clickable, redirecting to a URL in the same row?
问题
I can help you with that. You can achieve this by creating a clickable link for the 'Crypto' column using HTML formatting. Here's an example of how you can do it:
import pandas as pd
data = {
'Crypto': ['ARB', 'ETH', 'ETH'],
'Position': ['Long 10.0X', 'Short 25.0X', 'Short 2.0X'],
'Size Crypto': ['1,075,328.5024 ARB', '20.5007 ETH', '11.937 ETH'],
'Size USD': ['$1,240,929', '$37,225', '$21,675'],
'Entry Price': ['$1.15', '$1,793.4', '$1,810.4'],
'ROE': ['+ 3.94%', '- 31.24%', '- 0.60%'],
'URL': ['www.google.com', 'www.google.com', 'www.google.com'],
'Win Loss Ratio': [1.0, 1.0, 1.0],
'Last Price': ['1.152000', '1813.00', '1813.00']
}
# Create a DataFrame
df = pd.DataFrame(data)
# Replace the 'Crypto' column with clickable links
df['Crypto'] = df.apply(lambda row: f'<a href="http://{row["URL"]}">{row["Crypto"]}</a>', axis=1)
# Drop the 'URL' column
df.drop(columns=['URL'], inplace=True)
# Display the modified DataFrame
print(df)
This code replaces the 'Crypto' column with clickable links that redirect to the corresponding URLs in the 'URL' column and removes the 'URL' column from the DataFrame.
英文:
Suppose I have a pandas DF with the below data:
`data = {
'Crypto': ['ARB', 'ETH', 'ETH'],
'Position': ['Long 10.0X', 'Short 25.0X', 'Short 2.0X'],
'Size Crypto': ['1,075,328.5024 ARB', '20.5007 ETH', '11.937 ETH'],
'Size USD': ['$1,240,929', '$37,225', '$21,675'],
'Entry Price': ['$1.15', '$1,793.4', '$1,810.4'],
'ROE': ['+ 3.94%', '- 31.24%', '- 0.60%'],
'URL': ['www.google.com',
'www.google.com',
'www.google.com'],
'Win Loss Ratio': [1.0, 1.0, 1.0],
'Last Price': ['1.152000', '1813.00', '1813.00']
}`
I would like to get rid off the 'URL' column, but make it such that the 'Crypto' column is clickable and it redirects to the corresponding value under 'URL'.
Is this possible to achieve? I can't find anything in Google or the Pandas doc.
Thanks for your help
答案1
得分: 2
这是另一种方法来做:
(
df.assign(Crypto= df[["Crypto", "URL"]].apply(
lambda x: f'<a href="{x[1]}">{x[0]}</a>', axis=1))
.drop(columns="URL")
.style
)
输出:
英文:
Here is another way to do it :
(
df.assign(Crypto= df[["Crypto", "URL"]].apply(
lambda x: f'<a href="{x[1]}">{x[0]}</a>', axis=1))
.drop(columns="URL")
.style
)
Output :
答案2
得分: 1
你可以使用列表推导式和 pop
来删除列,然后根据输出处理方式选择要么转换 to_html
,要么使用 style
:
from IPython.display import HTML
df = pd.DataFrame(data)
df['Crypto'] = [f'<a href="{url}">{symbol}</a>'
for symbol, url in zip(df['Crypto'], df.pop('URL'))]
HTML(df.to_html(escape=False))
# 或者
df.style
输出:
英文:
You could use a list comprehension with pop
to remove the column, then either convert to_html
or use style
depending on how you want to handle the output:
from IPython.display import HTML
df = pd.DataFrame(data)
df['Crypto'] = [f'<a href="{url}">{symbol}</a>'
for symbol, url in zip(df['Crypto'], df.pop('URL'))]
HTML(df.to_html(escape=False))
# or
df.style
Output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论