在 pandas 中生成条件列
generate conditional column in pandas
我有:
pd.DataFrame({'price':['45p','£1.35']})
我想将它们转换成数字,得到:
pd.DataFrame({'price':['45p','£1.35'],'numeric':[0.45,1.35]})
我试过:
df['numeric']=np.where(df.price.str.contains('p') is True,
pd.to_numeric(df.price.str.replace('p',''))/100,
pd.to_numeric(df.price.str.replace('£','')))
并出现以下错误:ValueError: Unable to parse string "£1.35" at position 7
对我做错了什么有什么建议吗?
一次尝试一个步骤:
# where values in pennies
is_pennies = df['price'].str.contains('p')
# remove the currency characters and convert to numerics
df['price'] = df.price.str.replace('p|£', '').astype(float)
# update the values in pennies
df.loc[is_pennies, 'price'] /= 100
输出:
price
0 0.45
1 1.35
我有:
pd.DataFrame({'price':['45p','£1.35']})
我想将它们转换成数字,得到:
pd.DataFrame({'price':['45p','£1.35'],'numeric':[0.45,1.35]})
我试过:
df['numeric']=np.where(df.price.str.contains('p') is True,
pd.to_numeric(df.price.str.replace('p',''))/100,
pd.to_numeric(df.price.str.replace('£','')))
并出现以下错误:ValueError: Unable to parse string "£1.35" at position 7
对我做错了什么有什么建议吗?
一次尝试一个步骤:
# where values in pennies
is_pennies = df['price'].str.contains('p')
# remove the currency characters and convert to numerics
df['price'] = df.price.str.replace('p|£', '').astype(float)
# update the values in pennies
df.loc[is_pennies, 'price'] /= 100
输出:
price
0 0.45
1 1.35