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"]#, "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]
dfAsString = df.to_string(index=False)
print(dfAsString)
Current Code Output: (Truncated)
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
4 FART COIN $0.0000004281 1.13% 42.13% $42,806,075 $46,604
[30 rows x 6 columns]
How to produce the outpur sorted based on a particular column (24h)? -> Truncated
Name Price 1h 24h MarketCap Volume
0 DollarPepe $0.02752 22.64% 336.25% $3 $456,913
2 JEFF $0.1946 4.42% 226.18% $19,458,328 $19,744,583
1 Billy Token $0.00002822 41.69% 75.80% $1,958,942 $6,999,241
4 FART COIN $0.0000004281 1.13% 42.13% $42,806,075 $46,604
3 PUG AI $0.00000001459 10.80% 15.84% $1,459,428 $239,454
[30 rows x 6 columns]
I would convert all the numeric columns in your df to numeric values; then you can sort them easily (you can always add back $ and % as required on display).
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)
Output (for your sample data):
Name Price 1h 24h MarketCap Volume
0 DollarPepe 2.752000e-02 22.64 336.25 3 456913
2 JEFF 1.946000e-01 4.42 226.18 19458328 19744583
1 Billy Token 2.822000e-05 41.69 75.80 1958942 6999241
4 FART COIN 4.281000e-07 1.13 42.13 42806075 46604
3 PUG AI 1.459000e-08 10.80 15.84 1459428 239454
Note the non-numeric character replacement is more complicated than just [^\d.] as would be implied by your sample data; this is because some of the other price values fetched from that page have ... in them (presumably because they are too small to represent). Ideally you need to figure out how to get those as exact values; otherwise they can only be approximated.
Alternatively, you can sort on the values by converting them to floats before passing the series to sort_values:
df = df.sort_values('24h', ascending=False, key=lambda v:v.str.replace('%', '').astype(float))
Output:
Name Price 1h 24h MarketCap Volume
0 DollarPepe $0.02752 22.64% 336.25% $3 $456,913
2 JEFF $0.1946 4.42% 226.18% $19,458,328 $19,744,583
1 Billy Token $0.00002822 41.69% 75.80% $1,958,942 $6,999,241
4 FART COIN $0.0000004281 1.13% 42.13% $42,806,075 $46,604
3 PUG AI $0.00000001459 10.80% 15.84% $1,459,428 $239,454
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With