【问题标题】:How can I replace for loop of list in to dataframe column如何将列表的 for 循环替换为数据框列
【发布时间】:2022-01-11 05:24:15
【问题描述】:

我有数据框df_paths,我想从列路径中的列表中创建列 path_pair。输出选择如下表所示:

session_id path path_pair
T01 [Start, play_series, Null] [[Start, play_series],[ play_series, Null]]
T02 [Start, play_tv, purchase, Conversion] [[Start, play_tv],[play_tv,purchase],[purchase, Conversion]]

我的脚本:

for index, row in df_paths.iterrows():
  list_pair = []
  print(index, row['path'])

  for i, val in enumerate(row['path']):
    if i != len(row['path'])-1:
      row_ls = [row['path'][i], row['path'][i+1]]
      # append row list to ls
      list_pair.append(row_ls)
  print(list_pair)

  row['path_pair'] = np.array(row['path'])
  list_new = np.array(list_pair)

  df_paths.at[index, 'path_pair'] = row['path_pair'][list_new] 
  print(df_paths.loc[index]['path_pair'])

错误: IndexError: arrays used as indices must be of integer (or boolean) type

【问题讨论】:

  • 你能分享几个路径示例

标签: python arrays numpy indexing types


【解决方案1】:
data = [['T01', ['Start', 'play_series', 'Null']], [
    'T02', ['Start', 'play_tv', 'purchase', 'Conversion']]]
df = pd.DataFrame(data, columns=['session_id', 'path'])
df['path_pair'] = df['path'].apply(lambda x: list(zip(x, x[1:])))
df

【讨论】:

    猜你喜欢
    • 2021-08-21
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 2019-10-11
    • 2016-01-28
    • 2012-02-16
    • 1970-01-01
    • 2017-09-13
    相关资源
    最近更新 更多