【发布时间】:2021-10-14 12:09:52
【问题描述】:
假设我有一个 df 的 python 字符串:
string
0 this house has 3 beds inside
1 this is a house with 2 beds in it
2 the house has 4 beds
我想提取每个房子有多少张床。我觉得这样做的一个好方法是在beds 之前找到该项目。
在尝试解决这个问题时,我当然注意到字符串是按字符索引的。这意味着我必须将字符串转换为带有str.split(' ') 的列表。
然后,我可以在每个字符串中找到 'beds' 的索引,并返回之前的索引。我为此尝试了列表理解和df.iterrows(),但似乎无法找出正确的方法。我想要的输出是:
string beds
0 this house has 3 beds inside 3
1 this is a house with 2 beds in it 2
2 the house has 4 beds 4
【问题讨论】: