add_currency

add_currency(
    data=None,
    amount=None,
    from_=None,
    to='USD',
    into=None,
    rates=None,
)

Add a converted-currency column to a data frame.

Convenience wrapper around convert_currency() that appends a converted column to data. The source currency can be a fixed code or a per-row column: a from_ that names a column of data is read as a per-row source-currency column, while any other string (“EUR”) is a fixed code applied to every row. This is the common case where each respondent reported spend in their own currency. The new column defaults to <amount>_<to> (e.g. spend_usd); override with into.

R tells the two apart by quoting (a bare name is a column, a quoted string a constant); Python strings cannot, so a column name wins.

Parameters

Name Type Description Default
data pandas.DataFrame A data frame. If omitted, the session default is used. None
amount str The amount column to convert. None
from_ str Source currency: a column holding per-row codes (e.g. “currency”) or a constant code (e.g. “EUR”). None
to str Target currency code. Defaults to “USD”. 'USD'
into str Name of the new column. Defaults to "<amount>_<to>". None
rates dict or pandas.DataFrame Passed to convert_currency(). None

Returns

Name Type Description
pandas.DataFrame data with the converted column added.

See Also

convert_currency

Examples

>>> df = pd.DataFrame({"spend": [100, 200, 50], "currency": ["EUR", "GBP", "JPY"]})
>>> add_currency(df, "spend", from_="currency", to="USD")
>>> add_currency(df, "spend", from_="EUR", to="USD", into="spend_usd")