我将给出我正在尝试合并/转置的两个数据帧Search_Exits和Page_Exits的最小样本,以及我正在使用的代码。
Search_Exits
Search_Term No._of_Searches_before %_Search_Exits_before
hello 10 .070
goodbye 100 .030
Page_Exits
Search_Term Exit_Pages_actual Ratios
hello /store/car 0.30
hello /store/b2b 0.30
hello /store/catalog/product/12 0.40
goodbye /store/car 1.00
我希望在这里看到的结果是:
Search_Term No._of_Searches_before %_Search_Exits_before /store/car /store/catalog/product12 /store/catalog/product23 /store/b2b
hello 10 .070 0.30 0.40 0.00 0.30
goodbye 100 .030 1.00 0.00 0.00 0.00
我已经尝试了这个堆栈溢出问题的答案中给出的所有3个版本:How to merge two tables and transpose rows to columns,但所有版本都得到了相同的错误消息,我尝试了以下操作:
version 1
df = Search_Exits.merge(Page_Exits.groupby('Search_Term')['Exit_Pages_actual'].apply(lambda x: x.reset_index(drop=True)).unstack().reset_index())
version 2
Search_Exits.merge(Page_Exits.pivot_table(index='Search_Term', values='Ratios',columns='Exit_Pages_actual' + Page_Exits.groupby(['Search_Term'])['Exit_Pages_actual'].cumcount().astype(str)).reset_index())
version 3
(Search_Exits.set_index('Search_Term').join(Page_Exits.groupby('Search_Term')['Ratios'].apply(lambda x: x.tolist()).apply(pd.Series)).reset_index())
这三种方法都会给出以下错误,所以我不知道该怎么做,如果有人能帮上忙的话:
TypeError: '>' not supported between instances of 'int' and
'datetime.datetime'
更新:
因此,我尝试在我自己创建的模拟数据集上执行相同的操作,但我不再收到错误消息(所以我猜我不知道是什么导致了数据中的问题),但是我有两件事正在发生,这两件事与我希望它们发生的方式不同。首先,新生成的列没有使用我希望它们标记为的相应"Exit_Pages_actual“进行标记。其次,每一列并不代表只应归因于特定"Exit_Pages_actual“的比率,所以我想知道我应该如何处理代码来更改它,使其按我希望的方式工作?目前,对于我的新数据集,其余的内容大致如下:
Search_Term No._of_Searches_before %_Search_Exits_before 0 1 2 3
hello 10 .07 0.3 0.3 0.4 NaN
goodbye 100 .03 NaN 1.0 NaN
非数
发布于 2017-11-02 23:58:59
实际上,看起来我已经使用数据透视表找到了我想要的东西:
Page_Exits = Page_Exits.pivot(index='Search_Term',
columns='Exit_Pages_actual', values='Ratios').reset_index()
然后使用pd.merge...进行常规合并。
https://stackoverflow.com/questions/47076050
复制相似问题