在 Python 中合并和转置列会导致 TypeError: '>''int' 和 'datetime.datetime' 的实例之间不支持 [英] Merging and transposing columns in Python gives TypeError: '>' not supported between instances of 'int' and 'datetime.datetime'

查看:43
本文介绍了在 Python 中合并和转置列会导致 TypeError: '>''int' 和 'datetime.datetime' 的实例之间不支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将给出我尝试合并/转置的两个数据框 Search_Exits 和 Page_Exits 以及我正在使用的代码的最小样本.

I will give a minimum sample of the two dataframe, Search_Exits and Page_Exits that I am trying to merge/transpose and the code I am using.

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

我想在这里看到的结果是:

the result I would like to see here is:

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

我已经尝试了这个 stackoverflow 问题的答案中给出的所有 3 个版本:如何合并两个表并将行转置为列 但对所有这些表都得到相同的错误消息,我尝试了以下操作:

I have tried all 3 versions given in the answer from this stackoverflow question:How to merge two tables and transpose rows to columns but get the identical error message for all of them, I have attempted the following:

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()) 

所有这三个都给了我以下错误,所以我不知道该怎么办,如果有人可以提供帮助:

All 3 of these give me the following error, so I don't know what to do, if anyone can help:

TypeError: '>' not supported between instances of 'int' and 
'datetime.datetime'

更新:

所以我尝试在我自己创建的模拟数据集上做同样的事情,但我不再收到错误消息(所以我想我不知道是什么导致了数据中的问题)但是我有两件事目前发生的情况与我希望它们发生的方式不同.首先,新生成的列没有被标记为我希望它们被标记为相应的Exit_Pages_actual".其次,每一列不代表应该只归因于该特定Exit_Pages_actual"的比率,所以我想知道我应该用代码做什么来改变它,让它像我想要的那样工作?目前,使用我的新数据集,其余部分大致如下:

So I tried doing the same thing on a simulated dataset that I myself created and I no longer get the error message (so I guess I don't know what is causing that problem in the data) however I have 2 things that are currently occurring differently from how I want them to occur. Firstly, the newly generated columns are not being labeled with the corresponding "Exit_Pages_actual" that I wanted them to be labeled as. Secondly, each column does not represent the Ratios that should only be attributed to that particular "Exit_Pages_actual" so I was wondering what I should do with the code to change it for it to work like I want it to? Currently, with my new dataset the rest is something along the lines of:

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   

NaN

推荐答案

实际上,看起来我使用数据透视表找到了我想要的:

Actually, it looks like I figured out what I wanted using a pivot table:

Page_Exits = Page_Exits.pivot(index='Search_Term', 
columns='Exit_Pages_actual', values='Ratios').reset_index()

接着使用 pd.merge 进行常规合并....

followed by a regular merge using pd.merge....

这篇关于在 Python 中合并和转置列会导致 TypeError: '>''int' 和 'datetime.datetime' 的实例之间不支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆