如何从python中的pandas数据框中的列中提取关键字(字符串) [英] How to extract a keyword(string) from a column in pandas dataframe in python

查看:217
本文介绍了如何从python中的pandas数据框中的列中提取关键字(字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框df,它看起来像这样:

I have a dataframe df and it looks like this:

         id                        Type                        agent_id  created_at
0       44525   Stunning 6 bedroom villa in New Delhi               184  2018-03-09
1       44859   Villa for sale in Amritsar                          182  2017-02-19
2       45465   House in Faridabad                                  154  2017-04-17
3       50685   5 Hectre land near New Delhi                        113  2017-09-01
4      130728   Duplex in Mumbai                                    157  2017-02-07
5      130856   Large plot with fantastic views in Mumbai           137  2018-01-16
6      130857   Modern Design Penthouse in Bangalore                199  2017-03-24

我有此表格数据,我正尝试通过从列中提取关键字来清理此数据,从而用新列创建一个新的数据框.

I've this tabular data and I'm trying to clean this data by extracting keywords from the column and hence create a new dataframe with new columns.

Apartment  = ['apartment', 'penthouse', 'duplex']
House      = ['house', 'villa', 'country estate']
Plot       = ['plot', 'land']
Location   = ['New Delhi','Mumbai','Bangalore','Amritsar']

因此所需的数据帧应如下所示:

So the desired dataframe shoul look like this:

         id      Type        Location    agent_id  created_at
0       44525   House       New Delhi         184  2018-03-09
1       44859   House        Amritsar         182  2017-02-19
2       45465   House       Faridabad         154  2017-04-17
3       50685   Plot        New Delhi         113  2017-09-01
4      130728   Apartment      Mumbai         157  2017-02-07
5      130856   Plot           Mumbai         137  2018-01-16
6      130857   Apartment   Bangalore         199  2017-03-24

所以直到现在我都尝试过:

So till now i've tried this:

import pandas as pd
df = pd.read_csv('test_data.csv')

#i can extract these keywords one by one by using for loops but how
#can i do this work in pandas with minimum possible line of code.

for index, values in df.type.iteritems():
  for i in Apartment:
     if i in values:
         print(i)

df_new = pd. Dataframe(df['id'])

有人可以告诉我如何解决吗?

Can someone tell me how to solve this?

推荐答案

首先通过Location列.str.extract.html"rel =" nofollow noreferrer> str.extract|表示正则表达式OR:

First create Location column by str.extract with | for regex OR:

pat = '|'.join(r"\b{}\b".format(x) for x in Location)
df['Location'] = df['Type'].str.extract('('+ pat + ')', expand=False)

然后从另一个list创建字典,将值与键交换,并使用

Then create dictionary from another lists, swap keys with values and in loop set value by mask with str.contains and parameter case=False:

d = {'Apartment' : Apartment,
     'House' : House,
     'Plot' : Plot}

d1 = {k: oldk for oldk, oldv in d.items() for k in oldv}

for k, v in d1.items():
    df.loc[df['Type'].str.contains(k, case=False), 'Type'] = v

print (df)
       id       Type  agent_id  created_at   Location
0   44525      House       184  2018-03-09  New Delhi
1   44859      House       182  2017-02-19   Amritsar
2   45465      House       154  2017-04-17        NaN
3   50685       Plot       113  2017-09-01  New Delhi
4  130728  Apartment       157  2017-02-07     Mumbai
5  130856       Plot       137  2018-01-16     Mumbai
6  130857  Apartment       199  2017-03-24  Bangalore

这篇关于如何从python中的pandas数据框中的列中提取关键字(字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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