pandas.core.series.Series
:
Color
Red 4
Green 7
以及下面的多索引数据帧。我的目标是创造
Target
Value
数据帧中的列小于
熊猫.core.series.系列
价值
Value Target
Color Animal
Red Tiger 12 0
Tiger 3 1
Green Lion 6 1
Lion 35 0
我接下来的尝试
ValueError: Can only compare identically-labeled Series objects
import pandas as pd
import numpy as np
x = pd.Series([4,7], index=['Red','Green'])
x.index.name = 'Color'
dt = pd.DataFrame({'Color': ['Red','Red','Green','Green'], 'Animal': ['Tiger','Tiger','Lion','Lion'], 'Value': [12,3,6,35]})
dt.set_index(['Color','Animal'], inplace=True)
dt['Target'] = np.where(dt['Value'] < x ,1 ,0 )