1

I had been finding ways to tabulate the dataframes data according to what I need. I need ideas as to how to do it.

Code:

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))

Current Output:

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

Needed Ouput:

|    | 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  |
Drew Duazeh
  • 207
  • 6
  • If I get it correctly, you want to turn your floats in percentages. Could this be of any help? https://stackoverflow.com/questions/23981601/format-certain-floating-dataframe-columns-into-percentage-in-pandas – Para May 09 '23 at 08:41
  • Does this answer your question? [How do you control float formatting when using DataFrame.to\_markdown in pandas?](https://stackoverflow.com/questions/66236289/how-do-you-control-float-formatting-when-using-dataframe-to-markdown-in-pandas) –  May 09 '23 at 11:32

3 Answers3

2

I would do it this way :

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)
     )

​ Output :

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 |
Timeless
  • 22,580
  • 4
  • 12
  • 30
1

the only way that would work for me (using VS code) is applying a specific format to each specific column:

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))
  • Im getting error in the first line. `df['Price'] =df.Price.apply(lambda x: "{}".format(x) if abs(x - int(x)) < 1e-6 else x)`. – Drew Duazeh May 09 '23 at 21:17
0

you can try using float format in pandas -

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

You can set the precision in floatfmt according to your need.

Pravash Panigrahi
  • 701
  • 2
  • 7
  • 21