比较Python中的数据帧以计算差异

比较Python中的数据帧以计算差异,python,Python,我正在使用熊猫df抓取此网站()。我想得到一张硬币和价格的快照,然后在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, f

我正在使用熊猫df抓取此网站()。我想得到一张硬币和价格的快照,然后在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”的操作数类型不受支持


不知道为什么。欢迎任何帮助

您正在尝试分割两个字符串。显然没有语言会喜欢这样

如果你把两者都转换成浮点数,那就行了。有关转换列的信息,请参见此处:


但是-看看你的问题-你是否真的确定这就是你想要看价格增量的方式?您是否会考虑对现有服务器(例如Binance)使用其中一个API,并使用它们的代码流?

因为
价格
是一个字符串列。您可以使用该方法检查数据类型

您需要将字符串“$1234.5”转换为float。您可以使用以下参数执行此操作:

def parse_price(x):
    return float(x.replace('$', '').replace(',', ''))

url = "https://www.coingecko.com/en/coins/recently_added?page=1"
converters = {"Price": parse_price}

df = pd.read_html(requests.get(url).text, flavor="bs4", converters=converters)

请发布错误的完整堆栈跟踪。这是否回答了您的问题?很好,谢谢你的反馈
def parse_price(x):
    return float(x.replace('$', '').replace(',', ''))

url = "https://www.coingecko.com/en/coins/recently_added?page=1"
converters = {"Price": parse_price}

df = pd.read_html(requests.get(url).text, flavor="bs4", converters=converters)