【问题标题】:Using Pandas to merge similar data使用 Pandas 合并相似数据
【发布时间】:2020-07-13 22:20:58
【问题描述】:

如何将“推荐”等类似数据合并为一个值?

df['Why you choose us'].str.lower().value_counts()

location                           35
recommendation                     23
recommedation                       8
confort                             7
availability                        4
reconmmendation                     3
facilities                          3

【问题讨论】:

  • 您是否有方法确定单词是否相似,或者您是否要求一种算法来确定单词相似度?
  • 不是machine-learning 问题,请不要向无关标签发送垃圾邮件(已删除)。
  • @Craig 我要求一种算法来合并相似的条目名称(字符串),以便只有一个数字表示,例如,所有类似于“推荐”一词的条目将被一起计算作为唯一编号。

标签: python pandas dataframe data-science


【解决方案1】:

打印(df)

            reason  count
0         location     35
1   recommendation     23
2    recommedation      8
3          confort      7
4     availability      4
5  reconmmendation      3
6       facilities      3

.groupby(),部分字符串。.transform() 同时找到sum

df['groupcount']=df.groupby(df.reason.str[0:4])['count'].transform('sum')



          reason  count  groupcount
0         location     35          35
1   recommendation     23          34
2    recommedation      8          34
3          confort      7           7
4     availability      4           4
5  reconmmendation      3          34
6       facilities      3           3

如果需要并排查看字符串和部分字符串。试试

df=df.assign(groupname=df.reason.str[0:4])
df['groupcount']=df.groupby(df.reason.str[0:4])['count'].transform('sum')
print(df)


      reason  count groupname  groupcount
0         location     35      loca          35
1   recommendation     23      reco          34
2    recommedation      8      reco          34
3          confort      7      conf           7
4     availability      4      avai           4
5  reconmmendation      3      reco          34
6       facilities      3      faci           3

如果您像 csv 中一样连续有多个项目;那么

#Read csv
df=pd.read_csv(r'path')
#Create another column which is a list of values 'Why you choose us' in each row
df['Why you choose us']=(df['Why you choose us'].str.lower().fillna('no comment given')).str.split(',')
#Explode group to ensure each unique reason is int its own row but with all the otehr attrutes intact
df=df.explode('Why you choose us')
#remove any white spaces before values in the column group and value_counts
df['Why you choose us'].str.strip().value_counts()
print(df['Why you choose us'].str.strip().value_counts())

location            48
no comment given    34
recommendation      25
confort              8
facilities           8
recommedation        8
price                7
availability         6
reputation           5
reconmmendation      3
internet             3
ac                   3
breakfast            3
tranquility          2
cleanliness          2
aveilable            1
costumer service     1
pool                 1
comfort              1
search engine        1
Name: group, dtype: int64

【讨论】:

  • 好的,但是它是怎么找到相似词的呢?
  • 通过在我称之为原因的列中切片前 4 个字符来查找相似词。这是短语 df.reason.str[0:4]
  • 好的,所以它基本上是计算单词相似度的建议 - 当前 4 个字符相同时,单词是相似的。
  • 在这种情况下,切片 4 个字符的字符串给了我唯一分类所需的子字符串。查看我的编辑
  • 这有帮助吗?
猜你喜欢
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-22
  • 2020-10-25
  • 2021-02-04
  • 1970-01-01
相关资源
最近更新 更多