有条件地根据 pandas 中的其他列值填充列值 [英] Conditionally fill column values based on another columns value in pandas
问题描述
我有一个 DataFrame
,其中有几列。一列包含正在使用货币的符号,例如欧元或美元符号。另一列包含预算值。所以例如在一排它可能意味着5000欧元的预算,下一行它可以说2000美元的预算。
I have a DataFrame
with a few columns. One columns contains a symbol for which currency is being used, for instance a euro or a dollar sign. Another column contains a budget value. So for instance in one row it could mean a budget of 5000 in euro and in the next row it could say a budget of 2000 in dollar.
在熊猫我想为我的DataFrame添加一个额外的列,以欧元规范化预算。因此,基本上,对于每一行,如果货币列中的符号是欧元符号,则新列中的值应为预算列* 1中的值,而新列中的值应为预算列的值* 0.78125如果货币栏中的符号是美元符号。
In pandas I would like to add an extra column to my DataFrame, normalizing the budgets in euro. So basically, for each row the value in the new column should be the value from the budget column * 1 if the symbol in the currency column is a euro sign, and the value in the new column should be the value of the budget column * 0.78125 if the symbol in the currency column is a dollar sign.
我知道如何添加列,填充值,从另一列复制值等,但不如何根据另一列的值有条件地填充新列。
I know how to add a column, fill it with values, copy values from another column etc. but not how to fill the new column conditionally based on the value of another column.
任何建议?
推荐答案
你可能想做
You probably want to do
df['Normalized'] = np.where(df['Currency'] == '$', df['Budget'] * 0.78125, df['Budget'])
这篇关于有条件地根据 pandas 中的其他列值填充列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!