当名为“Keyword”的列中的值与相邻值重复时,我试图覆盖名为“Group”的列中的值。
例如,因为字符串“commercial office cleaning services”是重复的,所以我想将相邻的列覆盖为“commercial cleaning services”。
示例数据
所需输出
最小可重现示例
import pandas as pd
data = [
["commercial cleaning services", "commercial cleaning services"],
["commercial office cleaning services", "commercial cleaning services"],
["janitorial cleaning services", "commercial cleaning services"],
["commercial office services", "commercial cleaning"],
]
df = pd.DataFrame(data, columns=["Keyword", "Group"])
print(df)
我对Pandas还很陌生,不知道从哪里开始,我已经到了一个死胡同,谷歌和搜索堆栈溢出。
1条答案
按热度按时间taor4pac1#
IIUC,将
duplicated
与mask
和ffill
配合使用:#输出: