我想在新定义的“日期”列中组合年,月和日。我使用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检查了这是什么错误。但是我没有发现任何遗漏的列或空白错误。
答案 0 :(得分: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
但是,通过一些调整,您的方法仍然有效, ie 将axis
参数移到对apply
的调用中,而不是对{{1}的调用中}:
datetime