如何根据条件(NaN)插入新列[重复项]

问题描述 投票:0回答:1

此问题已经在这里有了答案:

我在Python上还很新,这是我的问题。这很基本!

我正在尝试根据另一列(称为“ Sector”)创建新列(称为“ SectorCode”)。例如,如果列“ Sector”包含“ Materials”,则它应在我的“ SectorCode”列上显示“ 10”。如果我有“ Industrials”,则为“ 20”,依此类推...

我发现了其他主题,但几乎总是包括基于像这样的数字的条件,而不是基于NaN的条件:https://www.dezyre.com/recipes/insert-new-column-based-on-condition-in-python(这就是我获得灵感来构建代码的地方)

这是我失败的代码:

import pandas as pd
import numpy as np
sector = pd.read_csv (r"C:\Users\alexa\sector.csv")
sector

dframe = pd.DataFrame(sector)
dframe.columns
Index(['Ticker', 'Sector'], dtype='object')


Sectorcode = []
for row in dframe['Sector']:
    if row = ('Energy') : Sectorcode.append(10)
        elif row = ('Materials') : Sectorcode.append(15)
        elif row = ('Industrials') : Sectorcode.append (20)
        elif row = ('Consumer Discretionary') : Sectorcode.append (25)
        elif row = ('Cosumer Staples') : Sectorcode.append (30)
        elif row = ('Health Care') : Sectorcode.append (35)
        elif row = ('Financials') : Sectorcode.append (40)
        elif row = ('Information Technology') : Sectorcode.append (45)
        elif row = ('Communication Services') : Sectorcode.append (50)
        elif row = ('Utilities'): Sectorcode.append (55)
        elif row = ('Real Estate'): Sectorcode.append (60)
        else : Sectorcode.append (0)
df['Sectorcode']= Sectorcode`

我收到此错误消息:

"  File "<ipython-input-8-98c65bbfd42a>", line 3
    if row = ('Energy') : Sectorcode.append(10)
       ^

SyntaxError:无效的语法“

我的实际表格如下:

         Ticker         Sector               
0         MSFT     Information Technology       
2         AAPL     Information Technology       
3         AMZN      Consumer Discretionary      
4         FB       Communication Services       
5         BRK.B        Financials               
6         XOM             Energy                
7         JNJ             Health Care     

等...

而且我想要这样的东西:

         Ticker         Sector                 SectorCode
0         MSFT     Information Technology       45
2         AAPL     Information Technology       45
3         AMZN      Consumer Discretionary      25
4         FB       Communication Services       50
5         BRK.B        Financials               40
6         XOM             Energy                10
7         JNJ             Health Care           35

等...

谢谢您的帮助! :)

编辑:

以下代码有效:

Sectorcode = []
for row in dframe['Sector']:
    if row == ('Energy') : Sectorcode.append(10)
    elif row == ('Materials') : Sectorcode.append(15)
    elif row == ('Industrials') : Sectorcode.append (20)
    elif row == ('Consumer Discretionary') : Sectorcode.append (25)
    elif row == ('Cosumer Staples') : Sectorcode.append (30)
    elif row == ('Health Care') : Sectorcode.append (35)
    elif row == ('Financials') : Sectorcode.append (40)
    elif row == ('Information Technology') : Sectorcode.append (45)
    elif row == ('Communication Services') : Sectorcode.append (50)
    elif row == ('Utilities'): Sectorcode.append (55)
    elif row == ('Real Estate'): Sectorcode.append (60)
    else : Sectorcode.append (0)
dframe['Sectorcode']= Sectorcode
python pandas conditional-statements
1个回答
0
投票

您使用一个=代表分配,当您需要使用两个==测试条件时。

© www.soinside.com 2019 - 2024. All rights reserved.