【问题标题】:KeyError: ('Year', 'occurred at index Year')KeyError: ('Year', '发生在索引 Year')
【发布时间】:2018-12-04 01:39:21
【问题描述】:

我想在新定义的日期列中组合年、月和日。我使用this link 来实现我的目标。我的数据框,名为 z 的数据框如下:

   Year  Month  day  Hour  Minute  Second   Latitude  Longirude  Exact  
0  1992     12   31    23      59      59  29.456137  85.506958      0   
1  2017     10    1     4      35      38  27.694225  85.291702      0   
2  2017     10    1     6      13      18  28.962729  80.912323      0   
3  2017     10    2     5      18      31  27.699097  85.299431      0   
4  2017     10    3     4      23      20  27.700438  85.329933      0

我的代码如下:

z['Date'] = z.apply(lambda row: datetime(int(row['Year']), int(row['Month']), int(row['day']), axis=1))

但是,它给了我错误:

Traceback (most recent call last):

  File "<ipython-input-40-3d0f2cb862d4>", line 1, in <module>
    z['Date'] = z.apply(lambda row: datetime(int(row['Year']), int(row['Month']), int(row['day']), axis=1))

  File "/usr/lib/python3/dist-packages/pandas/core/frame.py", line 3972, in apply
    return self._apply_standard(f, axis, reduce=reduce)

  File "/usr/lib/python3/dist-packages/pandas/core/frame.py", line 4064, in _apply_standard
    results[i] = func(v)

  File "<ipython-input-40-3d0f2cb862d4>", line 1, in <lambda>
    z['Date'] = z.apply(lambda row: datetime(int(row['Year']), int(row['Month']), int(row['day']), axis=1))

  File "/usr/lib/python3/dist-packages/pandas/core/series.py", line 557, in __getitem__
    result = self.index.get_value(self, key)

  File "/usr/lib/python3/dist-packages/pandas/core/index.py", line 1790, in get_value
    return self._engine.get_value(s, k)

  File "pandas/index.pyx", line 103, in pandas.index.IndexEngine.get_value (pandas/index.c:3204)

  File "pandas/index.pyx", line 111, in pandas.index.IndexEngine.get_value (pandas/index.c:2903)

  File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:3908)

KeyError: ('Year', 'occurred at index Year')

我还通过enter link description here检查了它是什么类型的错误。但我没有发现任何列缺失或空格错误。

【问题讨论】:

    标签: python-3.x datetime dataframe integer


    【解决方案1】:

    最好使用pd.to_datetime:

    z['Date'] = pd.to_datetime(df[['Year','Month','day']])
    
    >>> z['Date']
    0   1992-12-31
    1   2017-10-01
    2   2017-10-01
    3   2017-10-02
    4   2017-10-03
    Name: Date, dtype: datetime64[ns]
    

    通过这种方式,您可以获得一个易于与pandas date functionality 一起使用的日期时间序列

    但是,您的方式可行,只需稍作调整,axis 参数移动到对apply 的调用中,而不是对datetime 的调用中:

    from datetime import datetime
    
    z['Date'] = z.apply(lambda row: datetime(row['Year'], row['Month'], row['day']), axis=1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 2014-12-03
      • 2019-05-02
      • 1970-01-01
      相关资源
      最近更新 更多