在 pandas 中,如何在没有 .apply 的情况下乘以列的值?

How can i multiply values of a column without .apply in pandas?

我正在尝试将我数据集中一列中的每个值(例如 100-120 美元)转换为美元(有许多不同的货币,例如欧元等)因此,根据它们的货币,我需要将它们转换为各自的货币兑换率。 我的输入文件是这样的:

d = {'location': ['US', 'UK'], 'price': ['USD10-20', 'GBP10-20']}
df = pd.DataFrame(data=d)

位置|价格

美国 |10-20 美元

英国 |10-20 英镑

等等

我试过了:

def convertCurrency(price):
    c=CurrencyConverter()
    currency= price[0:3]
    numbers=re.findall(r'\d+',price)
    lowerbound= re.findall(r'\d+',price)[0]
    res=""
    upperbound='x'
    if currency=='USD':
        return price
    if len(numbers)>1:
        upperbound=numbers[1]
    first=int(c.convert(int(lowerbound),price,"USD"))
    if upperbound != 'x':
        second=int(c.convert(int(upperbound),price,"USD"))
        res=''+currency+str(first)+"-"+str(second)
    else:
        res = '' + currency + str(first)
    return res

并使用 apply

调用它
df['price'] = df.apply(lambda row: convertCurrency(row.price), axis=1)

但这花费的时间太长了。 我也试过这个:

df['price'] = convertCurrency(df['price'])

但这会引发错误,因为该函数获取的是系列对象而不是字符串。我必须改变什么或者还有其他方法吗? 我想要的结果是

位置|价格

美国 |10-20 美元

英国 |14-28 美元

让我们尝试在轴 1 上使用 extract to get usable values then apply

import pandas as pd
from currency_converter import CurrencyConverter

d = {'location': ['US', 'UK'], 'price': ['USD10-20', 'GBP10-20']}
df = pd.DataFrame(data=d)

c = CurrencyConverter()
# Extract Values
df[['currency', 'v1', 'v2']] = df['price'].str.extract(r'(\w{3})(\d+)-(\d+)',
                                                       expand=True)
# Mask For Non USD Currency
m = df['currency'].ne('USD')
# Replace price where not USD
df.loc[m, 'price'] = df[m].apply(
    lambda s: f'USD'
              f'{int(c.convert(int(s.v1), s.currency, "USD"))}'
              f'-'
              f'{int(c.convert(int(s.v2), s.currency, "USD"))}',
    axis=1
)
# Drop Added Columns
df = df.drop(columns=['currency', 'v1', 'v2'])
print(df)

输出:

  location     price
0       US  USD10-20
1       UK  USD13-27