Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将两列合并,将月份和年份分隔为一列,以进行天数的减法运算_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 将两列合并,将月份和年份分隔为一列,以进行天数的减法运算

Python 将两列合并,将月份和年份分隔为一列,以进行天数的减法运算,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我的数据框中有两列分开,一列是月份,另一列是商店收到竞争对手时的年份。我想做的是将这些列连接起来,然后从日期中减去,逐日得到值。但在运行我下面留下的代码时,我得到了一个错误,我无法揭示这意味着什么。我会让错误代码 # competition since df2['competition_since'] = df2.apply(lambda x: datetime( year=x['competition_open_since_year'], month=x['competition_open_s

我的数据框中有两列分开,一列是月份,另一列是商店收到竞争对手时的年份。我想做的是将这些列连接起来,然后从日期中减去,逐日得到值。但在运行我下面留下的代码时,我得到了一个错误,我无法揭示这意味着什么。我会让错误代码

# competition since
df2['competition_since'] = df2.apply(lambda x: datetime( year=x['competition_open_since_year'], month=x['competition_open_since_month'], day=1, axis=1 ))
df2['competition_time_month'] = ( ( df2['date'] - df2['competition_since'])/30 ).apply(lambda x: x.days).astype(int)
错误

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/opt/anaconda3/envs/sales_predict_rossmann/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3079             try:
-> 3080                 return self._engine.get_loc(casted_key)
   3081             except KeyError as err:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()

KeyError: 'competition_open_since_year'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-30-ac751656b323> in <module>
      1 # competition since
----> 2 df2['competition_since'] = df2.apply(lambda x: datetime( year=x['competition_open_since_year'], month=x['competition_open_since_month'], day=1, axis=1 ))
      3 df2['competition_time_month'] = ( ( df2['date'] - df2['competition_since'])/30 ).apply(lambda x: x.days).astype(int)

~/opt/anaconda3/envs/sales_predict_rossmann/lib/python3.8/site-packages/pandas/core/frame.py in apply(self, func, axis, raw, result_type, args, **kwds)
   7766             kwds=kwds,
   7767         )
-> 7768         return op.get_result()
   7769 
   7770     def applymap(self, func, na_action: Optional[str] = None) -> DataFrame:

~/opt/anaconda3/envs/sales_predict_rossmann/lib/python3.8/site-packages/pandas/core/apply.py in get_result(self)
    183             return self.apply_raw()
    184 
--> 185         return self.apply_standard()
    186 
    187     def apply_empty_result(self):

~/opt/anaconda3/envs/sales_predict_rossmann/lib/python3.8/site-packages/pandas/core/apply.py in apply_standard(self)
    274 
    275     def apply_standard(self):
--> 276         results, res_index = self.apply_series_generator()
    277 
    278         # wrap results

~/opt/anaconda3/envs/sales_predict_rossmann/lib/python3.8/site-packages/pandas/core/apply.py in apply_series_generator(self)
    288             for i, v in enumerate(series_gen):
    289                 # ignore SettingWithCopy here in case the user mutates
--> 290                 results[i] = self.f(v)
    291                 if isinstance(results[i], ABCSeries):
    292                     # If we have a view on v, we need to make a copy because

<ipython-input-30-ac751656b323> in <lambda>(x)
      1 # competition since
----> 2 df2['competition_since'] = df2.apply(lambda x: datetime( year=x['competition_open_since_year'], month=x['competition_open_since_month'], day=1, axis=1 ))
      3 df2['competition_time_month'] = ( ( df2['date'] - df2['competition_since'])/30 ).apply(lambda x: x.days).astype(int)

~/opt/anaconda3/envs/sales_predict_rossmann/lib/python3.8/site-packages/pandas/core/series.py in __getitem__(self, key)
    851 
    852         elif key_is_scalar:
--> 853             return self._get_value(key)
    854 
    855         if is_hashable(key):

~/opt/anaconda3/envs/sales_predict_rossmann/lib/python3.8/site-packages/pandas/core/series.py in _get_value(self, label, takeable)
    959 
    960         # Similar to Index.get_value, but we do not fall back to positional
--> 961         loc = self.index.get_loc(label)
    962         return self.index._get_values_for_loc(self, loc, label)
    963 

~/opt/anaconda3/envs/sales_predict_rossmann/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3080                 return self._engine.get_loc(casted_key)
   3081             except KeyError as err:
-> 3082                 raise KeyError(key) from err
   3083 
   3084         if tolerance is not None:

KeyError: 'competition_open_since_year'
---------------------------------------------------------------------------
KeyError回溯(最近一次呼叫最后一次)
获取位置中的~/opt/anaconda3/envs/sales\u predict\u rossmann/lib/python3.8/site-packages/pandas/core/index/base.py(self、key、method、tolerance)
3079尝试:
->3080自动返回引擎。获取锁定(铸造键)
3081除KeyError作为错误外:
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/index_class_helper.pxi在pandas._libs.index.Int64Engine._check_type()中
KeyError:“自年起竞争开始”
上述异常是以下异常的直接原因:
KeyError回溯(最近一次呼叫最后一次)
在里面
1#自
---->2 df2['competition\u since']=df2.apply(λx:datetime(年份=x['competition\u open\u since\u year'],月份=x['competition\u open\u since\u month'],日期=1,轴=1))
3 df2[“比赛时间/月份”]=(df2[“日期”]-df2[“比赛开始日期])/30)。申请(λx:x.days)。aType(int)
应用中的~/opt/anaconda3/envs/sales\u predict\u rossmann/lib/python3.8/site-packages/pandas/core/frame.py(self、func、axis、raw、result\u type、args、**kwds)
7766科威特第纳尔=科威特第纳尔,
7767         )
->7768返回操作获取结果()
7769
7770 def applymap(self、func、na_操作:可选[str]=None)->数据帧:
获取结果(self)中的~/opt/anaconda3/envs/sales\u predict\u rossmann/lib/python3.8/site-packages/pandas/core/apply.py
183返回自我。应用_原始()
184
-->185返回自我。应用标准()
186
187 def应用_空_结果(自身):
应用标准中的~/opt/anaconda3/envs/sales\u predict\u rossmann/lib/python3.8/site-packages/pandas/core/apply.py(self)
274
275 def应用_标准(自身):
-->276个结果,res_index=self.apply_系列_生成器()
277
278#总结结果
~/opt/anaconda3/envs/sales\u predict\u rossmann/lib/python3.8/site-packages/pandas/core/apply.py in apply\u series\u generator(self)
288表示枚举中的i、v(系列):
289#在此处使用copy忽略设置,以防用户发生变异
-->290结果[i]=self.f(v)
291如果存在(结果[i],ABC系列):
292#如果我们对v有看法,我们需要复制一份,因为
in(x)
1#自
---->2 df2['competition\u since']=df2.apply(λx:datetime(年份=x['competition\u open\u since\u year'],月份=x['competition\u open\u since\u month'],日期=1,轴=1))
3 df2[“比赛时间/月份”]=(df2[“日期”]-df2[“比赛开始日期])/30)。申请(λx:x.days)。aType(int)
~/opt/anaconda3/envs/sales\u predict\u rossmann/lib/python3.8/site-packages/pandas/core/series.py in\uuuuuuuu getitem\uuuuuuuuuuu(self,key)
851
852 elif键是标量:
-->853返回自我。获取值(键)
854
855如果可散列(键):
~/opt/anaconda3/envs/sales\u predict\u rossmann/lib/python3.8/site-packages/pandas/core/series.py in\u get\u value(自我、标签、可接受)
959
960#类似于Index.get_值,但我们不会退回到位置
-->961 loc=自索引获取位置(标签)
962返回self.index.\u获取\u loc(self,loc,label)的\u值\u
963
获取位置中的~/opt/anaconda3/envs/sales\u predict\u rossmann/lib/python3.8/site-packages/pandas/core/index/base.py(self、key、method、tolerance)
3080自动返回引擎。获取锁定(铸造键)
3081除KeyError作为错误外:
->3082从err升起钥匙错误(钥匙)
3083
3084如果公差不是无:
KeyError:“自年起竞争开始”

将lambda函数应用于数据帧时,会将列的值传递给它。因此,lambda函数的输入是数据帧的值。我想你是想做这样的事情:

import datetime

df2['competition_since'] = df2['competition_open_since_month'] + "/1/" + df2['competition_open_since_month']

df2['competition_since'] = df2['competition_since'].apply(lambda x: datetime.datetime.strptime(x, "%m/%d/%Y"))
在此之后,您可以从日期列中减去,以找出每天的差异。这样(我不认为你需要除以30):


KeyError表示“竞争\开放\自\年”列不存在。可能是打字错误?该列存在,我不知道为什么会返回此错误。有没有关于如何找到答案的建议?
(df2['date'] - df2['competition_since']).apply(lambda x: x.days)