熊猫货币转换

时间:2018-09-19 13:51:18

标签: python pandas

我在使用下面的代码时遇到麻烦。我的系统一旦开始执行下面的第三行,就会用完内存。我试图根据'M / D'列将金额转换为USD,该列表示是否需要乘以'LDIVND'金额或将'Rate'除以。 “ USDEQ”是我尝试创建并存储转换后金额的新列。

感谢您的协助。

t['M/D']=t['M/D'].astype(str)

t.loc[:,['LDIVND','Rate']]=t.loc[:,['LDIVND','Rate']].apply(pd.to_numeric)

t['USDEQ'] = t['M/D'].apply(
    lambda x: t['LDIVND']/t['Rate'] if x =='D' else t['LDIVND']*t['Rate'] )

1 个答案:

答案 0 :(得分:0)

使用numpy.where

t[['LDIVND','Rate']]=t[['LDIVND','Rate']].apply(pd.to_numeric)
t['USDEQ'] = np.where(t['M/D'].astype(str) =='D', 
                      t['LDIVND']/t['Rate'], 
                      t['LDIVND']*t['Rate'])