adlofMerlin
adlofMerlin

Reputation: 79

How to convert currency from the dataset which was know beforehand in Pandas Dataframe

I have a dictionary with the key is the date and the value is revenue from the date, as shown in the picture

Dictionary data:

enter image description here

I also have a dataframe of the currency of each date, as shown in the picture

Dataframe of the currency:

enter image description here

So, how to make a dataframe with 2 columns. The first columns is the date, and the second column is the converted revenue

Expected Values:

Date Revenue
2019-11-01 5853302
2019-11-02 17415125
2019-11-03 19684777
... ...
2020-03-29 1227610

Upvotes: 1

Views: 243

Answers (2)

tdy
tdy

Reputation: 41407

You can map() the revenues by id and multiply by idr.

Assuming the revenue dictionary is named revenue and the currency dataframe is named currency:

result = pd.DataFrame({
    'Date': currency.id,
    'Revenue': currency.id.astype(str).map(revenue) * currency.idr,
})

#       Date       Revenue
# 2019-11-01  5.853307e+06
# 2019-11-02  1.741513e+07
# 2019-11-03  1.968478e+07
# 2019-11-04  2.132557e+07
# ...

Upvotes: 1

Vedank Pande
Vedank Pande

Reputation: 446

this should work: (here df1 is your original dataframe)

df2 = pd.DataFrame(columns=['date','revenue'])

df2['date'] = df1['date']
df2['revenue'] = df1['idr'].apply(lambda x: x*10) #replace x*10 with whatever other calculation you want to do

Upvotes: 0

Related Questions