【问题标题】:How can I compare two different datasets and return values based in a column based on a specific criteria?如何比较两个不同的数据集并根据特定条件在列中返回值?
【发布时间】:2021-06-30 00:54:15
【问题描述】:

在比较“其他”数据框中的两个单独列之后,我试图在“计划”数据框中创建一个新列。这是我的代码现在的样子:

import pandas as pd
import numpy as np
schedule = pd.read_excel('schedule.xlsx')
other = pd.read_excel('other.xlsx')

other['New Column'] = np.where(other['Termination Date'] >= schedule['Beginning'] & (other['Termination Date'] <= schedule['End'], schedule['Pay Date']))

但它返回此错误:

ValueError: 只能比较标签相同的 Series 对象

以下是此场景中的典型示例:

如果

其他['终止日期']= "5/22/2021"

然后它将返回“6/11/2021”,因为它会查看

时间表['开始']

计划['结束']

满足标准。

请注意,这两个数据框没有任何相似的数据可以合并。基本上,我只需要比较一个数据帧并返回另一个数据帧的值。如果您有任何问题,请告诉我,并提前感谢大家!

schedule df

other df

【问题讨论】:

  • 您必须以某种方式合并数据集。能否提供schedule.xlsxother.xlsx 的样本?

标签: python pandas dataframe numpy


【解决方案1】:

看起来 Python 只会比较两个表中具有相同标签的列的值。您要求 Python 比较同一列两次。

也许尝试使用 if else 类型语句代替 &...

【讨论】:

  • 我向@rubberjesus 询问了一些样品
  • 没有汗水!很想听听你的进展!
  • 感谢大家的帮助。我发布了两个数据框外观的两个新屏幕截图@MarcusViniciusPompeu
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
相关资源
最近更新 更多