【发布时间】:2021-05-20 13:30:26
【问题描述】:
我正在使用 Pandas df 抓取这个网站 (https://www.coingecko.com/en/coins/recently_added?page=1)。我想获取代币和价格的快照,然后在 10 秒后获取代币和价格的另一个快照,以查看代币是否已按价格移动。这是我用来执行此操作的代码:
import pandas as pd
import requests
import time
url = "https://www.coingecko.com/en/coins/recently_added?page=1"
df = pd.read_html(requests.get(url).text, flavor="bs4")
df = pd.concat(df).drop(["Unnamed: 0", "Unnamed: 1"], axis=1)
# print(df.head)
df_first = df[['Coin', 'Price']]
time.sleep(10)
url = "https://www.coingecko.com/en/coins/recently_added?page=1"
df = pd.read_html(requests.get(url).text, flavor="bs4")
df = pd.concat(df).drop(["Unnamed: 0", "Unnamed: 1"], axis=1)
df_last = df[['Coin', 'Price']]
df_merged = df_first.merge(df_last, left_on=['Coin'], right_on=['Coin'])
df_merged.set_index(['Coin'], inplace=True)
df_merged['pct_change'] = df_merged[['Price_x','Price_y']].pct_change(axis=1)['Price_y']
df_merged['greater_than_1'] = df_merged['pct_change'] > 10.00
print(df_merged)
当我运行代码时出现错误
TypeError: /: 'str' 和 'str' 的操作数类型不受支持
不知道为什么。欢迎任何帮助
【问题讨论】:
-
请发布完整堆栈跟踪错误
-
这能回答你的问题吗? Converting strings to floats in a DataFrame
标签: python