资料集:
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列nd类别列表中找到匹配的单词:
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),..
最佳答案
遵循这些原则,应该可以为您工作:
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
)中。