根据条件创建新的熊猫列时发生ValueError

时间:2020-03-31 06:53:05

标签: python pandas dataframe

我正在尝试根据seasons列是否在两个日期之间创建熊猫数据框的新列date,并附上以下代码。

# Create a season column for the team attributes.
season_conds = [
('2008-08-01' <= df2.date <= '2009-06-01'),
('2009-08-01' <= df2.date <= '2010-06-01'),
('2010-08-01' <= df2.date <= '2011-06-01'),
('2011-08-01' <= df2.date <= '2012-06-01'),
('2012-08-01' <= df2.date <= '2013-06-01'),
('2013-08-01' <= df2.date <= '2014-06-01'),
('2014-08-01' <= df2.date <= '2015-06-01'),
('2015-08-01' <= df2.date <= '2016-06-01')]
seasons = df1.season.unique().tolist()
df2['season'] = np.select(season_conds, seasons)

但是,此代码抛出:

ValueError                                Traceback (most recent call last)
<ipython-input-162-4ecc76138e87> in <module>
      7 ('2012-08-01' <= df2.date <= '2013-06-01')|
      8 ('2013-08-01' <= df2.date <= '2014-06-01')|
----> 9 ('2014-08-01' <= df2.date <= '2015-06-01')|
     10 ('2015-08-01' <= df2.date <= '2016-06-01')]
     11 seasons = df1.season.unique().tolist()

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __nonzero__(self)
   1477     def __nonzero__(self):
   1478         raise ValueError(
-> 1479             f"The truth value of a {type(self).__name__} is ambiguous. "
   1480             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   1481         )

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

任何人都可以解释为什么会这样以及解决方案是什么吗?

0 个答案:

没有答案