How can I remove a column in a pandas DataFrame and make another column clickable, redirecting to a URL in the same row?

huangapple go评论63阅读模式
英文:

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 = {
    &#39;Crypto&#39;: [&#39;ARB&#39;, &#39;ETH&#39;, &#39;ETH&#39;],
    &#39;Position&#39;: [&#39;Long 10.0X&#39;, &#39;Short 25.0X&#39;, &#39;Short 2.0X&#39;],
    &#39;Size Crypto&#39;: [&#39;1,075,328.5024 ARB&#39;, &#39;20.5007 ETH&#39;, &#39;11.937 ETH&#39;],
    &#39;Size USD&#39;: [&#39;$1,240,929&#39;, &#39;$37,225&#39;, &#39;$21,675&#39;],
    &#39;Entry Price&#39;: [&#39;$1.15&#39;, &#39;$1,793.4&#39;, &#39;$1,810.4&#39;],
    &#39;ROE&#39;: [&#39;+ 3.94%&#39;, &#39;- 31.24%&#39;, &#39;- 0.60%&#39;],
    &#39;URL&#39;: [&#39;www.google.com&#39;,
            &#39;www.google.com&#39;,
            &#39;www.google.com&#39;],
    &#39;Win Loss Ratio&#39;: [1.0, 1.0, 1.0],
    &#39;Last Price&#39;: [&#39;1.152000&#39;, &#39;1813.00&#39;, &#39;1813.00&#39;]
}`

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
)

输出:

How can I remove a column in a pandas DataFrame and make another column clickable, redirecting to a URL in the same row?

英文:

Here is another way to do it :

(
    df.assign(Crypto= df[[&quot;Crypto&quot;, &quot;URL&quot;]].apply(
        lambda x: f&#39;&lt;a href=&quot;{x[1]}&quot;&gt;{x[0]}&lt;/a&gt;&#39;, axis=1))
            .drop(columns=&quot;URL&quot;)
            .style
)

Output :

How can I remove a column in a pandas DataFrame and make another column clickable, redirecting to a URL in the same row?

答案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

输出:

How can I remove a column in a pandas DataFrame and make another column clickable, redirecting to a URL in the same row?

英文:

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[&#39;Crypto&#39;] = [f&#39;&lt;a href=&quot;{url}&quot;&gt;{symbol}&lt;/a&gt;&#39;
                for symbol, url in zip(df[&#39;Crypto&#39;], df.pop(&#39;URL&#39;))]

HTML(df.to_html(escape=False))

# or
df.style

Output:

How can I remove a column in a pandas DataFrame and make another column clickable, redirecting to a URL in the same row?

huangapple
  • 本文由 发表于 2023年5月21日 23:32:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定