简体   繁体   English

根据pandas中的另一个列值有条件地填充列值

[英]Conditionally fill column values based on another columns value in pandas

I have a DataFrame with a few columns.我有一个包含几列的DataFrame 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.因此,例如在一行中,它可能表示 5000 欧元的预算,而在下一行中可能表示 2000 美元的预算。

In pandas I would like to add an extra column to my DataFrame, normalizing the budgets in euro.在 Pandas 中,我想在我的 DataFrame 中添加一个额外的列,以欧元标准化预算。 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.所以基本上,对于每一行,如果货币列中的符号是欧元符号,则新列中的值应该是预算列中的值 * 1,而新列中的值应该是预算列中的值 *如果货币列中的符号是美元符号,则为 0.78125。

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.我知道如何添加一列,用值填充它,从另一列复制值等,但不知道如何根据另一列的值有条件地填充新列。

Any suggestions?有什么建议?

你可能想做

df['Normalized'] = np.where(df['Currency'] == '$', df['Budget'] * 0.78125, df['Budget'])

Similar results via an alternate style might be to write a function that performs the operation you want on a row, using row['fieldname'] syntax to access individual values/columns, and then perform a DataFrame.apply method upon it通过替代样式的类似结果可能是编写一个函数来在一行上执行您想要的操作,使用row['fieldname']语法来访问单个值/列,然后对其执行DataFrame.apply方法

This echoes the answer to the question linked here: pandas create new column based on values from other columns这与此处链接的问题的答案相呼应: 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) 

Taking Tom Kimber's suggestion one step further, you could use a Function Dictionary to set various conditions for your functions.将 Tom Kimber 的建议更进一步,您可以使用函数字典为您的函数设置各种条件。 This solution is expanding the scope of the question.该解决方案正在扩大问题的范围。

I'm using an example from a personal application.我正在使用个人应用程序中的示例。

# 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.

相关问题 如何根据另一列中的单元格值有条件地填充 Pandas 列 - How to Conditionally Fill Pandas Column based on Cell Values in another column 熊猫:使用基于其他列值的函数有条件地填充列 - Pandas: Conditionally fill column using a function based on other columns values 根据熊猫df中其他列的值有条件地填充列 - Conditionally fill column based off values in other columns in a pandas df 根据另一列中的值有条件地填充 pandas 中的空值 - Filling null values in pandas based on value in another column conditionally 根据熊猫数据框中另一列的最后一个值填充列 - Fill columns based on the last value of another column in a pandas dataframe 根据其他列 pandas 中的值填入列 - Fill in column based on values in other columns pandas 根据熊猫中的行匹配,用另一个DataFrame中的值有条件地填充列 - Conditionally fill column with value from another DataFrame based on row match in Pandas 使用列表根据多列中的值有条件地填充新列 - Use a list to conditionally fill a new column based on values in multiple columns 在 pandas 中,如何根据一列中的唯一值创建列,然后根据另一列中的值填充它? - In pandas, how do I create columns out of unique values in one column, and then fill it based on values in another column? Pandas/Python:根据另一列中的值将列的值存储到列表中 - Pandas/Python: Store values of columns into list based on value in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM