使用Pandas DataFrame在网站URL上进行模式匹配 [英] Pattern matching on a website URL with Pandas DataFrame

查看:70
本文介绍了使用Pandas DataFrame在网站URL上进行模式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用模式匹配来解决网站网址的稍微复杂的项目。

I am trying to solve a slightly complex project with Pattern matching for a website URL.

我有一个特殊的列,其中包含URL,并在URL中嵌入了一些信息。我不能一直准确地得到这个错误,而且会不断出错。

I have a particular column that contains URL with several information embedded inside the URL. I am not getting this accurately I keep getting errors.

这里是我要解决的一个示例。我有一列名为 Google Analytics(分析)数据的页面。
它包含像这样的行 Mywebsite.com/optiona/optionb/type/102/103/107?data=1.2。 1581202800。1581289200.30

Here is an example of what I am trying to solve. I have a column called Page from Google analytics data. It contains rows like this Mywebsite.com/optiona/optionb/type/102/103/107?data=1.2. 1581202800. 1581289200.30

I want to create new columns containing 
the website - Mywebsite.com
option type a - optiona
option type b - optionb
type of product - type
product1 - 102
product2 - 103
product3 - 107

And ?data= in a separate column 
another new column - 1
new3 - 2
starttime - 1581202800
endtime - 1581289200
age - 30

我首先尝试将它们拆分为一个列表,以便我轻松访问它们:

I first tried to split them in a list so it is easy for me to access them:

df_analytic["Col_for_analysis"] = re.split(r"/",df_analytic["Page"])

但我得到: TypeError:预期的字符串或类似字节的对象

推荐答案

删除data =,除以您看到的所有内容:

Remove data=, split by everything you see:

df_split = df['input'].str.replace('data=', '').str.split(r' |/|\?|\.', expand=True).replace('', np.nan).dropna(how='all', axis=1)
then you can rename your columns as you wish.

编辑:我添加了空列。

Edit2:要考虑缺少的主机名,请分开分割:

to take into account the absent hostname, split separately:

df_split1 = df['input'].str.split(r'\?data=', expand=True)
df_left = df_split1.loc[:, 0].str.rsplit(r'/', n=5, expand=True)
df_right = df_split1.loc[:, 1].str.split(r'\.| ', expand=True)

df_left['option_a'] = df_left.iloc[:, 0].str.split(r'/', expand=True).iloc[:, -1].fillna(df_left.iloc[:, 0])
df_left['sitename'] = df_left.iloc[:, 0].apply(lambda x: np.NaN if '/' not in x else re.split(r'/', x)[0])

然后concat

df = pd.concat([df_left, df_right], axis=1).iloc[:, 1:].replace('', np.nan).dropna(how='all', axis=1)

然后对列进行其余的重命名。

then do the remaining renaming of the columns.

这篇关于使用Pandas DataFrame在网站URL上进行模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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