通过多种条件过滤数据框并在pandas中创建新列

时间:2020-04-07 14:34:52

标签: python pandas dataframe

尝试按2个条件过滤数据帧。对于“ Sub-Segment”列中等于“ Belux CCLM”的每一行,并且如果“ CV_LIF”列中的值介于0到26999之间,则创建一个新列,并将其标记为“ B”,并将其设置为“

但是我遇到了错误

'float' object is not subscriptable

def get_CV(row):
    if row['Sub-Segment']== 'Belux CCLM' and row['CV_LIF'] >= 0 and row['CV_LIF']<=26999:
        return '<$27K'
    if row['Sub-Segment']== 'Belux CCLM' and row['CV_LIF'] >=27000:
        return '<$27K'
    if row['Sub-Segment']== 'Belux CCLM' and row['CV_LIF']== ' ':
        return " "
df['B'] = df['CV_LIF'].apply(lambda x: get_CV(x))

0 个答案:

没有答案