如何根据条件(NaN)插入新列

时间:2019-10-26 18:24:28

标签: python pandas conditional-statements

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

我正在尝试根据另一列(称为“ 部门”)创建一个新列(称为“ 部门代码”)。 例如,如果列“ 扇区”包含“ 材料”,则它应在我的“ 扇区代码”列上显示“ 10”。如果我有“ 工业”,“ 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

1 个答案:

答案 0 :(得分:0)

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