Question:
I am trying to run a currency conversion function on a table so that if the currency in the each row isn’t already in the converted currency it changes it. I am using pandas, and have looked into the docs on numpy but have not been able to solve the issue.So far I have this:
I’ve been working on it for a little and this is how far I’ve gotten but I’m struggling to extract the current currency to plug into the method.
Answer:
Since both columns are needed as input, we can’t expect to just map aSeries
– we’d need to get corresponding values from the other column, and as you’ve noted, don’t have an obvious way to do that.Instead, let’s
.apply
on the entire DataFrame. Since we want to consider rows, we will use axis=1
. Then, our function will accept a row of the original, and can access the individual cells via the normal subscripting or attribute access.The function we want to use simply wraps
converter
to pass the cell values in the appropriate places, along with the hard-coded conv_currency
which is the same across the entire new column. Finally we can assign those apply
results to make the new column.Putting it all together gives something like:
If you have better answer, please add a comment about this, thank you!