2

数据集:

df['bigram'] = df['Clean_Data'].apply(lambda row: list(ngrams(word_tokenize(row), 2)))
df[:,0:1]

Id       bigram
1952043  [(Swimming,Pool),(Pool,in),(in,the),(the,roof),(roof,top),
1918916  [(Luxury,Apartments),(Apartments,consisting),(consisting,11),
1645751  [(Flat,available),(available,sale),(sale,Medavakkam),
1270503  [(Toddler,Pool),(Pool,with),(with,Jogging),(Jogging,Tracks),
1495638  [(near,medavakkam),(medavakkam,junction),(junction,calm),

我有一个 python 文件(Categories.py),其中包含财产/土地特征的无监督分类。

category = [('Luxury Apartments', 'IN', 'Recreation_Ammenities'),
        ('Swimming Pool', 'IN','Recreation_Ammenities'),
        ('Toddler Pool', 'IN', 'Recreation_Ammenities'),
        ('Jogging Tracks', 'IN', 'Recreation_Ammenities')]
Recreation = [e1 for (e1, rel, e2) in category if e2=='Recreation_Ammenities']

要从 bigram 列和类别列表中查找匹配的单词:

tokens=pd.Series(df["bigram"])
Lid=pd.Series(df["Id"])
matches = tokens.apply(lambda x: pd.Series(x).str.extractall("|".join(["({})".format(cat) for cat in Categories.Recreation])))

运行上述代码时,我收到此错误:

AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

在这方面需要帮助。

我想要的输出是:

 Id       bigram                                  Recreation_Amenities
1952043  [(Swimming,Pool),(Pool,in),(in,the),..   Swimming Pool
1918916  [(Luxury,Apartments),(Apartments,..      Luxury Apartments
1645751  [(Flat,available),(available,sale)..     
1270503  [(Toddler,Pool),(Jogging,Tracks)..      Toddler Pool,Jogging Tracks
1495638  [(near,medavakkam),..
4

2 回答 2

1

这些方面的东西应该适合你:

def match_bigrams(row):
    categories = []

    for bigram in row.bigram:
        joined = ' '.join(list(bigram))
        if joined in Recreation:
            categories.append(joined)

    return categories

df['Recreation_Amenities'] = df.apply(match_bigrams, axis=1)
print(df)


Id  bigram  Recreation_Amenities
0   1952043 [(Swimming, Pool), (Pool, in), (in, the), (the...   [Swimming Pool]
1   1918916 [(Luxury, Apartments), (Apartments, consisting...   [Luxury Apartments]
2   1645751 [(Flat, available), (available, sale), (sale, ...   []
3   1270503 [(Toddler, Pool), (Pool, with), (with, Jogging...   [Toddler Pool, Jogging Tracks]
4   1495638 [(near, medavakkam), (medavakkam, junction), (...   []

每个二元组都由一个空格连接,以便可以测试该二元组是否包含在您的类别列表中(即if joined in Recreation)。

于 2017-08-27T08:05:33.337 回答
1

您可以按空格加入元组,然后使用双重列表理解找到 Recreation 中存在的单词并应用即

df['Recreation_Amenities'] = df['bigram'].apply(lambda x : [j for j in Recreation if j in  [' '.join(i) for i in x]])

让我们考虑你有一个数据框

    标识符二元组
0 1270503 [(幼儿,游泳池),(游泳池,与),(与,慢跑),(慢跑,轨道)]
1 1952043 [(游泳,游泳池),(游泳池,中),(中,中),(中,屋顶),(屋顶,顶部)]
2 1918916 [(豪华, 公寓), (公寓, 包括), (包括, 11)]
3 1495638 [(近, medavakkam), (medavakkam, 路口), (路口, 平静)]
4 1645751 [(平,可用),(可用,出售),(出售,Medavakkam)]

你有娱乐清单,即

Recreation = ['Luxury Apartments', 'Swimming Pool', 'Toddler Pool', 'Jogging Tracks']

然后

df['Recreation_Amenities'] = df['bigram'].apply(lambda x : [j for j in Recreation if j in  [' '.join(i) for i in x]])

输出 :df['Recreation_Amenities']

0 [幼儿游泳池、慢跑跑道]
1 [游泳池]               
2【豪华公寓】           
3 []                            
4 []                            
名称:Recreation_Amenities,数据类型:对象

于 2017-08-27T08:44:03.033 回答