如何格式化Pandas数据框以在打印时添加边框?

dly7yett  于 2023-05-15  发布在  其他
关注(0)|答案(1)|浏览(232)

我想知道是否有任何可能的格式化模块,可用于格式化 Dataframe 输出。下面是代码:

import requests
import pandas as pd

pd.set_option("display.float_format", "{:.2f}".format)
url = "https://coinmarketcap.com/new/"
page = requests.get(url, headers={"User-Agent": "Mozilla/5.0"}, timeout=1)
pagedata = page.text
usecols = ["Name", "Price", "1h", "24h", "MarketCap", "Volume"]  # , "Blockchain"]

df = pd.read_html(pagedata)[0]  # Checking table
df[["Name", "Symbol"]] = df["Name"].str.split(r"\d+", expand=True)
df = df.rename(columns={"Fully Diluted Market Cap": "MarketCap"})[usecols]

numcols = df.columns[df.columns != "Name"]
df[numcols] = df[numcols].apply(
    lambda c: pd.to_numeric(c.str.replace(r"[^\d.]|(?<!\d)\.|\.(?!\d)", "", regex=True))
)
df = df.sort_values("24h", ascending=False)

formats = {
    "Price": "${:.2f}".format,
    "1h": "{:.2f}%".format,
    "24h": "{:.2f}%".format,
    "MarketCap": "${:,.2f}".format,
    "Volume": "{:,d}".format,
}
print(df.to_string(formatters=formats))

当前代码输出:

Name  Price     1h      24h         MarketCap     Volume
24              KEK  $0.00 13.66% 1116.77%    $17,577,074.00  7,545,426
4        DollarPepe  $0.03  5.57%  329.57%             $3.00    706,616
3      Diamond Pepe  $0.00 19.68%  301.20%    $10,737,494.00  2,116,732
21        FlokiPepe  $0.00 31.55%  117.41%     $1,446,299.00  1,475,708

所需产量:#--使用了markdown(),但不知何故改变了数字格式输出

|    | Name            | Price           | 1h     | 24h     | MarketCap      | Volume      |
|---:|:----------------|:----------------|:-------|:--------|:---------------|:------------|
|  0 | DollarPepe      | $0.02752        | 22.64% | 336.25% | $3             | $456,913    |
|  1 | Billy Token     | $0.00002822     | 41.69% | 75.80%  | $1,958,942     | $6,999,241  |
|  2 | JEFF            | $0.1946         | 4.42%  | 226.18% | $19,458,328    | $19,744,583 |
|  3 | PUG AI          | $0.00000001459  | 10.80% | 15.84%  | $1,459,428     | $239,454    |
2izufjch

2izufjch1#

使用您提供的dataframe:

import pandas as pd

df = pd.DataFrame(
    {
        "Name": ["KEK", "DollarPepe", "Diamond Pepe", "FlokiPepe"],
        "Price": ["$0.00", "$0.03", "$0.00", "$0.00"],
        "1h": ["13.66%", "5.57%", "19.68%", "31.55%"],
        "24h": ["1116.77%", "329.57%", "301.20%", "117.41%"],
        "MarketCap": ["$17,577,074.00", "$3.00", "$10,737,494.00", "$1,446,299.00"],
        "Volume": ["7,545,426", "706,616", "2,116,732", "1,475,708"],
    }
)

以下是使用Pandas str.padstyler.hide的一种方法:

df = df.reset_index()

# Setup a new dataframe and insert the first two rows
new_df = pd.DataFrame(columns=range(df.shape[1]))
new_df.loc[0] = [f"|{col}" if col != "index" else "| " for col in df.columns]
new_df.loc[1] = ["|:"] + ["|:-------------"] * (df.shape[1] - 1)

# Modify values in df and reset column header
for col in df.columns:
    df[col] = "|" + df[col].astype(str)
df.columns = range(df.shape[1])

# Concat dataframes and style new_df
new_df = pd.concat([new_df, df])
new_df["|"] = "|"
for col in new_df.columns[1:-1]:
    new_df[col] = new_df[col].str.pad(width=15, side="right")

然后,隐藏索引和列标题:

print(new_df.style.hide(axis=0).hide(axis=1).to_string())
# Output

|  |Name           |Price          |1h             |24h            |MarketCap      |Volume         |
|: |:------------- |:------------- |:------------- |:------------- |:------------- |:------------- |
|0 |KEK            |$0.00          |13.66%         |1116.77%       |$17,577,074.00 |7,545,426      |
|1 |DollarPepe     |$0.03          |5.57%          |329.57%        |$3.00          |706,616        |
|2 |Diamond Pepe   |$0.00          |19.68%         |301.20%        |$10,737,494.00 |2,116,732      |
|3 |FlokiPepe      |$0.00          |31.55%         |117.41%        |$1,446,299.00  |1,475,708      |

相关问题