简体   繁体   English

append 列的值基于 dataframe 中另一列的值

[英]append column with values based on values of another column in the dataframe

# Sample of existing Dataframe
data = {'portfolio': ['40/60', '60/40', '80/20', '100/0']}

test_df = pd.DataFrame(data)

# print dataframe.
test_df


Output:

portfolio
40/60
60/40
80/20
100/20

I am trying to create new columns to include the name of the portfolio and model name based on the value in the existing column.我正在尝试根据现有列中的值创建新列以包含投资组合的名称和 model 名称。 Note: there are other several other rows that include these existing values/ratios that make up a complete allocation of each portfolio.注意:还有其他几行包含这些现有值/比率,它们构成了每个投资组合的完整分配。 So I need the portfolio name to align with the corresponding value in the "Portfolio" column.所以我需要投资组合名称与“投资组合”列中的相应值保持一致。 Here is the current code I am using below:这是我在下面使用的当前代码:

def test(df):
    column1 = []
    column2 = []
    for row in df['Portfolio']:
        if row == '40/60':
            column1.append('Portfolio 1')
            column2.append('Portfolio 1 Model')
        elif row == '60/40':
            column1.append('Portfolio 2')
            column2.append('Portfolio 2 Model')
        elif row == '80/20':
            column1.append('Portfolio 3')
            column2.append('Portfolio 3 Model')
        elif row == '100/0':
            column1.append('Portfolio 4')
            column2.append('Portfolio 4 Model')
        else:
            column1.append('N/A')
            column2.append('N/A')

    df['portfolio_name'] = column1
    df['model_name'] = column2

    return df

test(test_df)

Expected output:

portfolio.   portfolio_name.  model_name
40/60        Portfolio 1      Portfolio 1 Model
60/40        Portfolio 2      Portfolio 2 Model
80/20        Portfolio 3      Portfolio 3 Model
100/0        Portfolio 4      Portfolio 4 Model


Actual Output:

portfolio.   portfolio_name.  model_name
40/60        N/A              N/A
60/40        N/A              N/A
80/20        N/A              N/A
100/0        N/A              N/A

I am just not sure what I am missing here and why the values appending to the newly created columns are only recognizing the "else" condition?我只是不确定我在这里缺少什么以及为什么附加到新创建的列的值只识别“else”条件?

I think the problem is in the iterable of the for loop, you should use this instead:我认为问题出在 for 循环的可迭代中,您应该改用它:

for i, row in df['Portfolio'].items():

Try to map your values with a dictionary:尝试使用字典 map 您的值:

names = {'40/60': {'portfolio_name': 'Portfolio 1', 'model_name': 'Portfolio 1 Model'},
         '60/40': {'portfolio_name': 'Portfolio 2', 'model_name': 'Portfolio 2 Model'},
         '80/20': {'portfolio_name': 'Portfolio 3', 'model_name': 'Portfolio 3 Model'},
         '100/0': {'portfolio_name': 'Portfolio 4', 'model_name': 'Portfolio 4 Model'},}

df = df.join(df['Portfolio'].map(names).dropna().apply(pd.Series))
print(df)

# Output
  Portfolio portfolio_name         model_name
0     40/60    Portfolio 1  Portfolio 1 Model
1     60/40    Portfolio 2  Portfolio 2 Model
2     80/20    Portfolio 3  Portfolio 3 Model
3     100/0    Portfolio 4  Portfolio 4 Model
4     50/50            NaN                NaN

Setup:设置:

data = {'Portfolio': ['40/60', '60/40', '80/20', '100/0', '50/50']}
df = pd.DataFrame(data)
data = {'portfolio': ['40/60', '60/40', '80/20', '100/0']}
df = pd.DataFrame(data)
df['portfolioName'] = "portfolio " + df.index.map(str)
df['modelName'] = "portfolio " + df.index.map(str) + " Model"
df = df.set_index('portfolio')

Results in the following结果如下

   portfolio  portfolioName   modelName
        
    40/60     portfolio 0     portfolio 0 Model
    60/40     portfolio 1     portfolio 1 Model
    80/20     portfolio 2     portfolio 2 Model
    100/0     portfolio 3     portfolio 3 Model

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM