gpt4 book ai didi

python - 将字典中的键与字符串匹配

转载 作者:太空宇宙 更新时间:2023-11-03 13:08:28 26 4
gpt4 key购买 nike

大家好!

dict = {'Awesome' : 'Sauce', 'Foo' : 'Barr'}

Col A Col B
1 'This is Awesome'
2 'I really foo him'

我正在尝试找到最符合 Python 风格的方法来遍历数据集/帧并返回与字典值匹配的任何字符串。

例如,对于数字 1,我想在 c 列中返回 Sauce,在相应的行中返回 2 'barr',但在 col c 中。

如果重要的话,我正在处理一个 csv/excel 文件。

如有任何帮助,我们将不胜感激。我很高兴使用 Pandas 和 NP 库。

为文编辑:

ID     Name of Course
0 Super Event Training: English Event...
1 Start with our Maths Training...
2 Live online Biology Training...
3 Maths throughout time...
4 Online Q&A Webinar: History..
5 Start with our Creative ...
6 Spring Conf with Author
7 Physics in our age ...
8 Spring Conf
9 Start with our educational items...
10 Education delivery in India...
11 English IELTS, Access to University..
12 Our Core Products for Empowerment..

我有一个这样的 DF,它持续了大约 500 行,我在 API 的帮助下进行了抓取,我需要将这个自由格式的文本转换为我的字典中的值。我所做的是确定关键字,我将这些关键字放入我的键值中并分配给字典值,这样我们就可以分析数据。

也许使用字典不是最好的方法?任何建议将不胜感激。

DN。

最佳答案

你只能使用python

[''.join(z) for z in [[y[1] if y[0] in x  else '' for x in df['Col B'] ] for y in d.items()]]
Out[22]: ['Sauce', 'Barr']

关于python - 将字典中的键与字符串匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50139187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com