pandas 如何使用markdown格式化dataframe中的浮点值

bzzcjhmw  于 2023-05-12  发布在  其他
关注(0)|答案(3)|浏览(133)

我一直在寻找根据我的需要将dataframes数据制成表格的方法。我需要关于如何做这件事的想法。
验证码:

import requests
import pandas as pd

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"]

df = pd.read_html(pagedata)[0] 
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)

print(df.head(5).to_markdown(index=True))

电流输出:

|    | Name       |     Price |    1h |     24h |   MarketCap |   Volume |
|---:|:-----------|----------:|------:|--------:|------------:|---------:|
| 17 | Homer      | 0.002555  | 39.21 | 1026.37 |     1072959 |  3206658 |
|  8 | minionseth | 1.476e-09 | 18.63 |  364.24 |     1476161 |  2724346 |
| 20 | MEME       | 0.002241  |  6.92 |  222.47 |     9414284 |   823402 |
| 14 | Bambi      | 9.65e-08  |  0.86 |  153.54 |     9650263 | 10457780 |
| 29 | TIGGER     | 0.002378  |  2.48 |   83.69 |      237836 |   961658 |

所需输出:

|    | Name       |     Price      |    1h  |     24h   |   MarketCap   |   Volume   |
|---:|:-----------|---------------:|-------:|----------:|--------------:|-----------:|
| 17 | Homer      | 0.002555       | 39.21% | 1,026.37% |     1,072,959 |  3,206,658 |
|  8 | minionseth | 0.000000001476 | 18.63% |   364.24% |     1,476,161 |  2,724,346 |
| 20 | MEME       | 0.002241       |  6.92% |   222.47% |     9,414,284 |   823,402  |
| 14 | Bambi      | 0.0000000965   |  0.86% |   153.54% |     9,650,263 | 10,457,780 |
| 29 | TIGGER     | 0.002378       |  2.48% |    83.69% |      237,836  |   961,658  |
jdgnovmf

jdgnovmf1#

我会这样做:

df = pd.read_html(page.text)[0]

df[["Name", "Symbol"]] = df["Name"].str.split(r"\d+", expand=True)
​
df = (df.rename(columns={"Fully Diluted Market Cap": "MarketCap"})[usecols]
          .sort_values("24h", ascending=False, key=lambda ser: ser.str.replace("%", "").astype(float))
          .replace(r"^\$", "", regex=True)
     )

输出:

print(df.head(5).to_markdown(index=True))
​
|    | Name       | Price          | 1h     | 24h      | MarketCap   | Volume     |
|---:|:-----------|:---------------|:-------|:---------|:------------|:-----------|
| 18 | Homer      | 0.0...03865    | 17.12% | 1604.14% | 1,623,332   | 3,519,028  |
|  9 | minionseth | 0.000000002347 | 21.67% | 638.19%  | 2,347,217   | 2,203,097  |
| 21 | MEME       | 0.0...00195    | 2.16%  | 180.50%  | 8,189,003   | 812,746    |
| 15 | Bambi      | 0.00000009247  | 7.98%  | 142.95%  | 9,247,233   | 10,308,548 |
| 23 | Capybara   | 0.0001335      | 28.76% | 112.01%  | 9,212,468   | 15,826,995 |
oyt4ldly

oyt4ldly2#

你可以尝试在pandas中使用float format-

print(df.to_markdown(floatfmt=".2f"))

您可以根据需要以floatfmt为单位设置精度。

guz6ccqo

guz6ccqo3#

对我来说唯一可行的方法(使用VS代码)是对每个特定的列应用特定的格式:

df['Price'] =df.Price.apply(lambda x: "{:,.12f}".format(x) if abs(x - int(x)) < 1e-6 else x)
df['1h'] = df['1h'].apply(lambda x: "{:,.2f}%".format(x))
df['24h'] = df['24h'].apply(lambda x: "{:,.2f}%".format(x))
df['MarketCap'] = df['MarketCap'].apply(lambda x: "{:,}".format(x))
df['Volume'] = df['Volume'].apply(lambda x: "{:,}".format(x))

相关问题