【问题标题】:Creating a Pandas column based on values of other columns [duplicate]根据其他列的值创建 Pandas 列[重复]
【发布时间】:2022-01-17 22:39:19
【问题描述】:

我想根据其他列的值创建一个 Pandas 列。以下是我的代码:

import pandas as pd
import numpy as np

raw_data = {'col1': [23,45,21],'col2': ['we', 'ee', 'cv'],'col3': ['y1', 'y2', 'y3']}
df = pd.DataFrame(raw_data, columns = ['col1','col2','col3'])
df['newCol'] = [
        'N1' if c1 == 'we' else
            'N2' if c1 == 'ee' | np.isin(c2, ['y2', 'y3']) else
                'N3'
    for c1, c2 in zip(df['col2'], df['col3'])
]

以上代码因错误而失败

TypeError: ufunc 'bitwise_or' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

您能帮我理解一下我的代码出了什么问题吗?

【问题讨论】:

  • 搜索np.selectnp.where

标签: python python-3.x pandas


【解决方案1】:

可以使用np.select(list of conditions, listof choices, alternative choice)

df['new']=np.select([df['col2']=='we',(df['col2']=='ee')|(df['col3'].isin(['y2','y3']))],['N1','N2'],'N3')



    col1 col2 col3 new
0    23   we   y1  N1
1    45   ee   y2  N2
2    21   cv   y3  N2

【讨论】:

  • 这比OP的原代码效率高很多
【解决方案2】:

你需要or这个词(和一些括号):

import pandas as pd
import numpy as np
raw_data = {'col1': [23,45,21],'col2': ['we', 'ee', 'cv'],'col3': ['y1', 'y2', 'y3']}
df = pd.DataFrame(raw_data, columns = ['col1','col2','col3'])
df['newCol'] = [
        'N1' if c1 == 'we' else
            'N2' if (c1 == 'ee' or np.isin(c2, ['y2', 'y3'])) else
                'N3'
    for c1, c2 in zip(df['col2'], df['col3'])
]

【讨论】:

    【解决方案3】:

    修复你的代码

    df['newCol'] = [
        'N1' if c1 == 'we' else
        'N2' if c1 == 'ee' or c2 in ['y2', 'y3'] else
        'N3'
        for c1, c2 in zip(df['col2'], df['col3'])
    ]
    df
       col1 col2 col3 newCol
    0    23   we   y1     N1
    1    45   ee   y2     N2
    2    21   cv   y3     N2
    

    【讨论】:

      【解决方案4】:
      raw_data = {'col1': [23,45,21],'col2': ['we', 'ee', 'cv'],'col3': ['y1', 'y2', 'y3']}
      df = pd.DataFrame(raw_data, columns = ['col1','col2','col3'])
      df['newCol'] = [
              'N1' if c1 == 'we' else
                  'N2' if c1 == 'ee' or (np.isin(c2, ['y2', 'y3'])) else
                      'N3'
          for c1, c2 in zip(df['col2'], df['col3'])
      ]
      

      你需要分开操作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-19
        • 2022-10-13
        • 2020-08-18
        • 1970-01-01
        • 1970-01-01
        • 2020-09-24
        • 1970-01-01
        • 2020-05-30
        相关资源
        最近更新 更多