簡體   English   中英

系列對象是可變的,因此它們不能在 Python pandas 數據幀上散列

[英]Series objects are mutable, thus they cannot be hashed on Python pandas dataframe

我有以下數據框:

df1:

          Revenue    Earnings       Date
Year
2017  43206832000  4608790000 2017-01-01
2018  43462740000  8928258000 2018-01-01
2019  44268171000  5001014000 2019-01-01
2020  43126472000  4770527000 2020-01-01

我正在使用api來獲取excahnge貨幣,api是CurrencyConverter,鏈接是: https ://pypi.org/project/CurrencyConverter/

我正在嘗試向我的數據框中添加一列以顯示該日期的匯率,我使用了以下方法:

c.convert(100, 'EUR', 'USD', date=date(2013, 3, 21))

我的代碼是:

c = CurrencyConverter()
earnings['exchange_rate'] = c.convert(1, 'BRL', 'USD', earnings['Date'])
print(earnings)

我得到的答案是:

TypeError: 'Series' objects are mutable, thus they cannot be hashed

我想得到以下內容:

        Revenue    Earnings       Date    exchange_rate
Year
2017  43206832000  4608790000 2017-01-01  0.305
2018  43462740000  8928258000 2018-01-01  0.305
2019  44268171000  5001014000 2019-01-01  0.295
2020  43126472000  4770527000 2020-01-01  0.249

嘗試:

from currency_converter import CurrencyConverter

# if "Date" column isn't already converted:
df["Date"] = pd.to_datetime(df["Date"])

c = CurrencyConverter(fallback_on_missing_rate=True)    # without fallback_on_missing_rate=True I get `BRL has no rate for 2017-01-01` error.
df["exchange_rate"] = df["Date"].apply(lambda x: c.convert(1, "BRL", "USD", x))
print(df)

印刷:

          Revenue    Earnings       Date  exchange_rate
Year                                                   
2017  43206832000  4608790000 2017-01-01       0.306034
2018  43462740000  8928258000 2018-01-01       0.304523
2019  44268171000  5001014000 2019-01-01       0.258538
2020  43126472000  4770527000 2020-01-01       0.249114

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM