【问题标题】:"The truth value of a Series is ambiguous." New column in DataFrame based on comparing on list“一个系列的真值是模棱两可的。”基于列表比较的DataFrame中的新列
【发布时间】:2019-05-10 13:56:41
【问题描述】:

我已经查看了很多关于该错误的问题,但我没有找到任何可以帮助解决我的问题的方法。 我有 DataFrame

Errors.dtypes

日期对象
小时 int64
分钟 int64
第二个 int64
机器对象
定位对象
ErrorVal 对象
持续时间 int64
数据类型:对象

列表列表

list_of

[[datetime.date(2019, 1, 27), 'MAS1', 'OBS', '15'],
[datetime.date(2019, 1, 10), 'MAS1', 'OBS', '21'],
...

现在,我想根据 list_ofErrors 中添加新列 - 当列 'Date'、'Machine'、'Position'、'ErrorVal' 在list_of - 新列“AboveAv”的值为 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_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

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']]

我的新数据框:

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

【问题讨论】:

  • 如错误消息所述,您必须将.empty.bool().item().any().all() 之一附加到您的系列。例如,..., Errors['ErrorVal']] in tmp).all() else False

标签: python pandas


【解决方案1】:

您可以制作另一个 DataFrame 并将它们合并。

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  

确保 Dtype 相同,否则这将不起作用!!!检查 Error.info 和 df.info 以获得比 Error.dtypes 和 df.dtypes 更具体的结果。

【讨论】:

    猜你喜欢
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多