![](/img/trans.png)
[英]How to Conditionally Fill Pandas Column based on Cell Values in another column
[英]Conditionally fill column values based on another columns value in pandas
我有一个包含几列的DataFrame
。 一列包含使用货币的符号,例如欧元或美元符号。 另一列包含预算值。 因此,例如在一行中,它可能表示 5000 欧元的预算,而在下一行中可能表示 2000 美元的预算。
在 Pandas 中,我想在我的 DataFrame 中添加一个额外的列,以欧元标准化预算。 所以基本上,对于每一行,如果货币列中的符号是欧元符号,则新列中的值应该是预算列中的值 * 1,而新列中的值应该是预算列中的值 *如果货币列中的符号是美元符号,则为 0.78125。
我知道如何添加一列,用值填充它,从另一列复制值等,但不知道如何根据另一列的值有条件地填充新列。
有什么建议?
你可能想做
df['Normalized'] = np.where(df['Currency'] == '$', df['Budget'] * 0.78125, df['Budget'])
通过替代样式的类似结果可能是编写一个函数来在一行上执行您想要的操作,使用row['fieldname']
语法来访问单个值/列,然后对其执行DataFrame.apply方法
这与此处链接的问题的答案相呼应: pandas create new column based on values from other columns
def normalise_row(row):
if row['Currency'] == '$'
...
...
...
return result
df['Normalized'] = df.apply(lambda row : normalise_row(row), axis=1)
将 Tom Kimber 的建议更进一步,您可以使用函数字典为您的函数设置各种条件。 该解决方案正在扩大问题的范围。
我正在使用个人应用程序中的示例。
# write the dictionary
def applyCalculateSpend (df_name, cost_method_col, metric_col, rate_col, total_planned_col):
calculations = {
'CPMV' : df_name[metric_col] / 1000 * df_name[rate_col],
'Free' : 0
}
df_method = df_name[cost_method_col]
return calculations.get(df_method, "not in dict")
# call the function inside a lambda
test_df['spend'] = test_df.apply(lambda row: applyCalculateSpend(
row,
cost_method_col='cost method',
metric_col='metric',
rate_col='rate',
total_planned_col='total planned'), axis = 1)
cost method metric rate total planned spend
0 CPMV 2000 100 1000 200.0
1 CPMV 4000 100 1000 400.0
4 Free 1 2 3 0.0
一个不需要额外导入numpy
:
df['Normalized'] = df['Budget'].where(df['Currency']=='$', df['Budget'] * 0.78125)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.