【问题标题】:How to insert a new column based on condition (NaN) [duplicate]如何根据条件(NaN)插入新列[重复]
【发布时间】:2020-02-22 16:36:30
【问题描述】:

我对 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

【问题讨论】:

  • 使用字典,如d = {'Energy':10, 'Materials':15, ...} 和地图。

标签: python pandas conditional-statements


【解决方案1】:

当您需要使用两个== 来测试一个条件时,您使用一个= 代表赋值。

【讨论】:

  • 谢谢你!我用 == 替换了 = 并纠正了另外两个小错误,它奏效了!
猜你喜欢
  • 1970-01-01
  • 2013-03-31
  • 2022-01-19
  • 1970-01-01
  • 2021-02-01
  • 2023-04-06
  • 2019-01-28
  • 2019-09-02
  • 2012-07-03
相关资源
最近更新 更多