【发布时间】:2018-08-06 12:13:34
【问题描述】:
我想将“短语”栏中的复数词转换为单数词。如何遍历每一行和每一项?
my_data = [('Audi Cars', 'Vehicles'),
('Two Parrots', 'animals'),
('Tall Buildings', 'Landmark')]
test = pd.DataFrame(my_data)
test.columns = ["Phrase","Connection"]
test
我试过了
test["Phrase"] = test["Phrase"].str.lower().str.split()
import inflection as inf
test["Phrase"].apply(lambda x:inf.singularize([item for item in x]))
我想要的输出是
Phrase: Connection:
Audi Car Vehicles
Two Parrot animals
Tall Building Landmark
请注意,我只想单数化一列Phase
【问题讨论】:
-
它可以与
test["Phrase"].apply(lambda x:[inf.singularize(item) for item in x])一起使用吗? -
@xdze2 谢谢,是的,它有效,但是我也必须加入这些话。所以下面 vivek 提供的答案可以按预期工作
-
@xdze2 正如您在列表理解中的评论中所解释的
x必须是x.split()然后是' '.join()外部 -
怎么样
rstip(),像test['Phrase'] = test['Phrase'].apply(lambda x:x.rstrip("s"))这样的东西?