Python 熊猫为什么要这样做?

Python 熊猫为什么要这样做?,python,pandas,apply,Python,Pandas,Apply,我正在“结果日期”列上应用一个函数,该函数将更改其日期格式 def change_date_format(row): old_date = row['Outcome Date'] old_date_reformatted = datetime.datetime.strptime(old_date, '%m/%d/%Y %H:%M').strftime('%Y-%m-%d %H:%M') row['Outcome Date'] = old_date_reformatte

我正在“结果日期”列上应用一个函数,该函数将更改其日期格式

def change_date_format(row):

    old_date = row['Outcome Date']
    old_date_reformatted  = datetime.datetime.strptime(old_date, '%m/%d/%Y %H:%M').strftime('%Y-%m-%d %H:%M')
    row['Outcome Date'] = old_date_reformatted

    return row


ffn = os.path.join(new_ciq_root, filename)

in_df = pd.read_csv(ffn, encoding="ISO-8859-1")
in_df[col_name] = in_df.apply(lambda row: change_date_format(row), axis=1)

我在apply函数中放置了一个断点,它到达最后一行,该行的“结果日期”似乎被正确地重新格式化(下面的屏幕截图)

但最终结果不是一个具有正确重新格式化的“结果日期”列的DF,而是将“结果日期”替换为“结果类型”列的值。我做错了什么

提示? 每次迭代后,我的调试器在C:\Users\aidenm\AppData\Local\Programs\Python\Python37-32\Lib\site packages\pandas\core\apply.py中遇到以下异常

    def apply_standard(self):

        # try to reduce first (by default)
        # this only matters if the reduction in values is of different dtype
        # e.g. if we want to apply to a SparseFrame, then can't directly reduce

        # we cannot reduce using non-numpy dtypes,
        # as demonstrated in gh-12244
        if (self.result_type in ['reduce', None] and
                not self.dtypes.apply(is_extension_type).any()):

            # Create a dummy Series from an empty array
            from pandas import Series
            values = self.values
            index = self.obj._get_axis(self.axis)
            labels = self.agg_axis
            empty_arr = np.empty(len(index), dtype=values.dtype)
            dummy = Series(empty_arr, index=index, dtype=values.dtype)

            try:
                result = reduction.reduce(values, self.f,
                                          axis=self.axis,
                                          dummy=dummy,
                                          labels=labels)
                return self.obj._constructor_sliced(result, index=labels)
            except Exception:
                pass
  • apply
    之后,您将获得一个新的
    df
    ,但您将其分配给了[col\u name]中的旧
  • 您应该
    df=df.apply(…)

  • lambda行:更改日期格式(行)
    与传递
    change\u date\u格式
    lambda是冗余的

  • 在您的情况下,在单个
    系列
    上而不是在行上应用该函数会更好、更优雅:

  • in_-df[col\u-name]=in_-df[col\u-name]。应用(更改日期格式1(col),axis=0)

    在这种情况下,您的函数
    change\u date\u format1
    应该是:


    lambda x:datetime.datetime.strtime(x,“%m/%d/%Y%H:%m”).strftime(“%Y-%m-%d%H:%m”)

    谢谢您的回答。我刚刚用一个真正的lambda 1线性行替换了它,它位于_df['impdt']=in_df[col_name].astype(str).apply(lambda x:datetime.datetime.strtime(x,'%m/%d/%Y').strftime('%Y-%m-%d%H:%m'))
    。我有点困惑b/c我从其他代码复制了这个代码结构,它似乎在工作。“工作”的代码看起来像“def normalise_row(row):if row['Acct']='xxx':k=row['OnID']v=Acct_OnID_map[k]如果Acct_OnID_map中的k没有其他行['Acct']=v return row result['Acct']=result.apply(lambda row:normalise_row(row),axis=1)``无法复制所有代码,但似乎是相同的模式