(python) 只获取某一列的句子,根据同一行不同列的值
(python) Only get the sentences in a column, based on a value of a different column in the same row
我正在尝试根据不同的列拆分我的数据框 (df_example) 的一列。我只想获取包含不同列值的句子。在 Python 中执行此操作的 best/cleanest 方法是什么?在下面的示例中,我附上了我希望在 desired_outcome 列中看到的内容。
期待帮助。
enter image description here
这不是您想要的,因为我不使用 pandas,但它工作正常。我希望你会知道适应你的利益。
resources/get_text.txt的内容:
filter_value,text,desired_outcome
flower,This is a flower. It has amazing colors.,This is a flower.
tree,This is not a flower. It is a tree. The tree is brown.,It is a tree. The tree is brown.
car,Flying with a car is crazy. You should drive it. It smells like a flower
脚本:
import csv
if __name__ == '__main__':
with open("resources\get_text.txt", 'r') as f:
dict_reader = csv.DictReader(f)
headers = dict_reader.fieldnames
for item in dict_reader:
target = item['filter_value']
sentences = item['text'].split('. ')
available_sentences = [_ for _ in sentences if target in _]
output = '. '.join(available_sentences)
if not output.endswith('.'):
output = f"{output}."
print(output)
assert output == item['desired_outcome']
数据:
>>> df = pd.read_csv("data.csv")
>>> df
FIlter_value text
0 flower This is a flower. It has amazing colors.
1 tree This is no flower. It is a tree. The tree is b...
2 car Flying with a car is crazy. You should drive i...
>>> df['desired_column'] = (df.apply(
... lambda x: '.'.join(
... [i for i in x.text.split(".") if x.FIlter_value in i]
... ) ,
... axis=1
... ).to_list())
>>> df
FIlter_value text desired_column
0 flower This is a flower. It has amazing colors. This is a flower
1 tree This is no flower. It is a tree. The tree is b... It is a tree. The tree is broun
2 car Flying with a car is crazy. You should drive i... Flying with a car is crazy
我正在尝试根据不同的列拆分我的数据框 (df_example) 的一列。我只想获取包含不同列值的句子。在 Python 中执行此操作的 best/cleanest 方法是什么?在下面的示例中,我附上了我希望在 desired_outcome 列中看到的内容。
期待帮助。
enter image description here
这不是您想要的,因为我不使用 pandas,但它工作正常。我希望你会知道适应你的利益。
resources/get_text.txt的内容:
filter_value,text,desired_outcome
flower,This is a flower. It has amazing colors.,This is a flower.
tree,This is not a flower. It is a tree. The tree is brown.,It is a tree. The tree is brown.
car,Flying with a car is crazy. You should drive it. It smells like a flower
脚本:
import csv
if __name__ == '__main__':
with open("resources\get_text.txt", 'r') as f:
dict_reader = csv.DictReader(f)
headers = dict_reader.fieldnames
for item in dict_reader:
target = item['filter_value']
sentences = item['text'].split('. ')
available_sentences = [_ for _ in sentences if target in _]
output = '. '.join(available_sentences)
if not output.endswith('.'):
output = f"{output}."
print(output)
assert output == item['desired_outcome']
数据:
>>> df = pd.read_csv("data.csv")
>>> df
FIlter_value text
0 flower This is a flower. It has amazing colors.
1 tree This is no flower. It is a tree. The tree is b...
2 car Flying with a car is crazy. You should drive i...
>>> df['desired_column'] = (df.apply(
... lambda x: '.'.join(
... [i for i in x.text.split(".") if x.FIlter_value in i]
... ) ,
... axis=1
... ).to_list())
>>> df
FIlter_value text desired_column
0 flower This is a flower. It has amazing colors. This is a flower
1 tree This is no flower. It is a tree. The tree is b... It is a tree. The tree is broun
2 car Flying with a car is crazy. You should drive i... Flying with a car is crazy