Python &引用;级数的真值是模糊的;基于列表比较的DataFrame中的新列
我已经检查了很多关于这个错误的问题,但是我没有发现任何可以帮助我解决问题的东西。 我有数据帧:Python &引用;级数的真值是模糊的;基于列表比较的DataFrame中的新列,python,pandas,Python,Pandas,我已经检查了很多关于这个错误的问题,但是我没有发现任何可以帮助我解决问题的东西。 我有数据帧: Errors.dtypes [[datetime.date(2019, 1, 21), 'MAS1', 'POS', '14'], [datetime.date(2019, 1, 22), 'MAS1', 'POS', '76'], [datetime.date(2019, 1, 27), 'MAS1', 'POS', '76']] 日期对象 小时int64 分钟int64 第二个int64
Errors.dtypes
[[datetime.date(2019, 1, 21), 'MAS1', 'POS', '14'],
[datetime.date(2019, 1, 22), 'MAS1', 'POS', '76'],
[datetime.date(2019, 1, 27), 'MAS1', 'POS', '76']]
日期对象小时int64
分钟int64
第二个int64
机器对象
定位对象
ErrorVal对象
持续时间int64
数据类型:对象 和列表列表列表:
list_of
Date Hour Minute Second Machine Position ErrorVal Duration
1 2019-01-12 22 50 30 MAS1 POS 76 94
2 2019-01-14 3 13 21 MAS1 POS 76 87
3 2019-01-21 3 14 54 MAS1 POS 14 19
4 2019-01-22 3 59 57 MAS1 POS 76 87
5 2019-01-25 4 1 30 MAS1 POS 14 12
6 2019-01-27 11 15 28 MAS1 POS 76 63
[[datetime.date(2019年1月27日),'MAS1','OBS','15'],[日期时间.日期(2019年1月10日),'MAS1','OBS','21'],
现在,我想根据的列表在的错误中添加新列-当列“日期”、“机器”、“位置”、“错误值”位于的列表中时-新列“高于AV”的值为True,否则为False。我尝试了以下操作:
Errors['AboveAv'] = True if ([Errors['Date'],Errors['Machine'],Errors['Position'],Errors['ErrorVal']] in tmp) else False
但是,当我尝试运行此操作时出错:ValueError:序列的真值不明确。请使用a.empty、a.bool()、a.item()、a.any()或a.all()
我如何处理它?我只想得到一个新的列,如果这一行包含在list\u的
示例:
数据帧错误:
list_of
Date Hour Minute Second Machine Position ErrorVal Duration
1 2019-01-12 22 50 30 MAS1 POS 76 94
2 2019-01-14 3 13 21 MAS1 POS 76 87
3 2019-01-21 3 14 54 MAS1 POS 14 19
4 2019-01-22 3 59 57 MAS1 POS 76 87
5 2019-01-25 4 1 30 MAS1 POS 14 12
6 2019-01-27 11 15 28 MAS1 POS 76 63
列表:
Errors.dtypes
[[datetime.date(2019, 1, 21), 'MAS1', 'POS', '14'],
[datetime.date(2019, 1, 22), 'MAS1', 'POS', '76'],
[datetime.date(2019, 1, 27), 'MAS1', 'POS', '76']]
我的新数据帧:
Date Hour Minute Second Machine Position ErrorVal Duration AboveAv
1 2019-01-12 22 50 30 MAS1 POS 76 94 False
2 2019-01-14 3 13 21 MAS1 POS 76 87 False
3 2019-01-21 3 14 54 MAS1 POS 14 19 True
4 2019-01-22 3 59 57 MAS1 POS 76 87 True
5 2019-01-25 4 1 30 MAS1 POS 14 12 False
6 2019-01-27 11 15 28 MAS1 POS 76 63 True
您可以创建另一个数据帧并合并它们
list_of = [[datetime.date(2019, 1, 21), 'MAS1', 'POS', '14'],
[datetime.date(2019, 1, 22), 'MAS1', 'POS', '76'],
[datetime.date(2019, 1, 27), 'MAS1', 'POS', '76']]
df = pd.DataFrame(list_of, columns=['Date', 'Machine', 'Position', 'ErrorVal'])
df['AboveAv'] = True
Error = pd.merge(Error, df, on=['Date', 'Machine', 'Position', 'ErrorVal'], how='left')
Error.fillna(False)
结果
Date Hour Minute Second Machine Position ErrorVal Duration \
0 2019-01-12 22 50 30 MAS1 POS 76 94
1 2019-01-14 3 13 21 MAS1 POS 76 87
2 2019-01-21 3 14 54 MAS1 POS 14 19
3 2019-01-22 3 59 57 MAS1 POS 76 87
4 2019-01-25 4 1 30 MAS1 POS 14 12
5 2019-01-27 11 15 28 MAS1 POS 76 63
AboveAv
0 False
1 False
2 True
3 True
4 False
5 True
确保数据类型相同,否则将无法工作!!!请检查Error.info和df.info以获得比Error.Dtypes和df.Dtypes更具体的结果。正如错误消息所说,您必须附加
.empty
,.bool(),.item()
,.any()
或.all()中的一个
添加到您的系列中。例如,…,tmp中的错误['ErrorVal']]。all()否则为False
。