0

I want to combine the Year, Month, and Day in the newly defined column Date. I used this link to achieve my goal. My data frame, named z has dataframe as below:

   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

my code is as follows :

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

But, it gave me error:

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

I also checked what kind of error it is through enter link description here. But I didn't find any colum missing or whitespace wrong.

sacuL
  • 49,704
  • 8
  • 81
  • 106
Mala Pokhrel
  • 143
  • 2
  • 2
  • 19

1 Answers1

1

Best is to use 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]

In this way you get a datetime series that is easy to use with pandas date functionality

However, your way works, with a little bit of tweaking, i.e. moving the axis argument into the call to apply rather than the call to datetime:

from datetime import datetime

z['Date'] = z.apply(lambda row: datetime(row['Year'], row['Month'], row['day']), axis=1)
sacuL
  • 49,704
  • 8
  • 81
  • 106