我一直在寻找根据我的需要将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 |
3条答案
按热度按时间jdgnovmf1#
我会这样做:
输出:
oyt4ldly2#
你可以尝试在pandas中使用
float format
-您可以根据需要以
floatfmt
为单位设置精度。guz6ccqo3#
对我来说唯一可行的方法(使用VS代码)是对每个特定的列应用特定的格式: