【问题标题】:ValueError: Incompatible indexer with Series - trying to fix valuesValueError:与 Series 不兼容的索引器 - 尝试修复值
【发布时间】:2021-09-06 01:40:36
【问题描述】:

谁能帮我解决这个问题?

我得到了以下数据框:

enter image description here

SEGSTART 列表示客户调用的确切日期和时间。如果此客户在过去 7 天内调用了两次或更多次,则必须将其视为 RECALL_7d(值 = 1)。如果不是,则 RECALL_7d 值必须设置为 0。

但是,RECALL_7d 系列包含一些错误,因为有时它会将客户视为召回,有时则不会。我需要解决这个问题,我这样做了:

  1. 首先我创建了一个名为 days 的列来计算第 i 行和第 i+1 行的 segstart 之间的差异:

    df['days'] = df.SEGSTART.diff().apply(lambda x: x/np.timedelta64(1,'D')).fillna(0).astype('int64')

上面的代码返回了以下数据框。可以看出,它包含一个“错误”,因为索引 6 的天数应该是 0。所以,为了解决这个问题,我尝试这样做:

size = len(df['Client_id'])
df['days'] = np.nan

for i in range (size-1):

    if df.loc[i+1, 'Client_id'] == df.loc[i, 'Client_id']:
        df.loc[i+1, 'days'] = df.loc[:i+1, 'SEGSTART'].diff().apply(lambda x: x/np.timedelta64(1, 'D')).fillna(0).astype('int64')

    else:
        df.loc[i+1, 'days'] = 0

所以我得到了这个错误:

ValueError Traceback(最近一次调用最后一次) 在 5 6 如果 df.loc[i+1, 'Client_id'] == df.loc[i, 'Client_id']: ----> 7 df.loc[i+1, 'days'] = df.loc[:i+1, 'SEGSTART'].diff().apply(lambda x: x/np.timedelta64(1, 'D')).fillna(0).astype('int64') 8 9 其他:

~\anaconda3\lib\site-packages\pandas\core\indexing.py in setitem(self, key, value) 668 669 iloc = self if self.name == "iloc" else self.obj.iloc --> 670 iloc._setitem_with_indexer(索引器,值) 671 第672章

~\anaconda3\lib\site-packages\pandas\core\indexing.py in _setitem_with_indexer(self, indexer, value) 1640 第1641章 -> 1642 值 = self._align_series(索引器,值) 1643 第1644章

~\anaconda3\lib\site-packages\pandas\core\indexing.py in _align_series(self, indexer, ser, multiindex_indexer) 1952 返回 ser.reindex(ax)._values 1953年 -> 1954 年引发 ValueError(“与系列不兼容的索引器”) 1955年 1956 def _align_frame(self, indexer, df: ABCDataFrame):

ValueError:与 Series 不兼容的索引器

enter image description here

【问题讨论】:

    标签: python pandas dataframe data-science


    【解决方案1】:

    DATA 列上使用pd.to_datetime 以启用日期和时间增量之间的处理和比较。然后,使用groupby('Client_id') 并对DATA 值进行排序(如果尚未排序),这将简化每个客户端的召回计算。使用 diff,与 7 天时间段 (pd.Timedelta(7, 'D')) 进行比较,并定义这是否被视为召回。使用pd.concatsort_index() 重建原始数据框。

    import pandas as pd
    
    df = pd.read_csv('sample.csv')
    df['DATA'] = pd.to_datetime(df['DATA'])
    
    full_df = []
    for n, g in df.groupby('Client_id'):
        g['DATA'] = g.DATA.sort_values()
        g['recall_7d'] = (g.DATA.diff() < pd.Timedelta(7, 'D')).astype(int)
        full_df.append(g)
    
    df_out = pd.concat(full_df).sort_index()
    print(df_out)
    

    df_out的输出

          Client_id                 SEGSTART       DATA  recall_7d
    0   38034991852  2021-01-08 20:15:05.000 2021-01-08          0
    1   38034991852  2021-01-27 19:57:49.000 2021-01-27          0
    2   38034991852  2021-01-27 19:37:33.000 2021-01-27          1
    3   38034991852  2021-01-27 22:08:58.000 2021-01-27          1
    4   38034991852  2021-02-01 10:45:00.000 2021-02-01          1
    5   38034991852  2021-02-01 10:55:53.000 2021-02-01          1
    6   11111111111  2021-02-03 10:15:05.000 2021-02-03          0
    7   11111111111  2021-02-06 20:23:07.000 2021-02-06          1
    8   11111111111  2021-02-07 08:54:05.000 2021-02-07          1
    9   11111111111  2021-02-07 20:17:05.000 2021-02-07          1
    10  11111111111  2021-02-13 16:25:00.000 2021-02-13          1
    11  11111111111  2021-02-28 17:19:23.000 2021-02-28          0
    

    【讨论】:

      猜你喜欢
      • 2016-10-15
      • 1970-01-01
      • 2018-03-08
      • 2021-10-12
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多