Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

python - Currency Conversion Dataframe - skip Columns

I am retrieving Yahoo stock ticker data and want to convert the given currency to euros. For this purpose I am using the Python Library Currency Converter and the pandas method multiply.

One of the columns, trading volume, shouldn't be "converted" - whats the best way to skip it? This is what I currently have:

import pandas as pd
import datetime
import pandas_datareader.data as web
from pandas import Series, DataFrame
from currency_converter import CurrencyConverter

start = datetime.datetime(2017, 1, 1)
end = datetime.datetime(2020, 12, 31)


c = CurrencyConverter()

df = web.DataReader("EXK", 'yahoo', start, end)
df.tail()

conversion = c.convert(1, 'USD', 'EUR')

eurodf = df.multiply(conversion,axis='rows')
eurodf.tail()

One approach I thought of taking, was to maybe join the "volume" column after multiplication. Alternatively I could just target that one column and convert it back?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can loc all columns except one. For example:

   A  B  C
0  0  1  2
1  3  4  5
2  6  7  8

df.loc[:, df.columns.drop('B')] *= 10

Result:

    A  B   C
0   0  1  20
1  30  4  50
2  60  7  80

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...