我这个数据框:
Receipt Description Card Member Account Cost
200a apple adam 08203928 $2
20022a pear bob 08203228 $7
202a orange alice 0820321228 $8
Run Code Online (Sandbox Code Playgroud)
我想检查description列中的值是否包含特定的子字符串。例如,第一行 (adam) 的描述是“apple”。我想检查此description列中是否存在子字符串“appl” 。
如果是这样,我想创建一个名为的新列Data,然后将存储值need more apples。如果未找到“appl”的子字符串,我不想在此列中存储任何内容。
这就是预期的新数据框的样子。
Receipt Description Card Member Account Cost **Data**
200a apple adam 08203928 $2 need more apples
20022a pear bob 08203228 $7
202a orange alice 0820321228 $8
Run Code Online (Sandbox Code Playgroud)
你可以试试这个:
示例 1:
df["**Data**"] = df["Description"].map(lambda x: "apple containes" if "appl" in x else '')
Run Code Online (Sandbox Code Playgroud)
示例 2
如果您要检查每个水果的映射,那么您可以像这样创建
desc = {"appl":"need more apples","pear": "need more pear"}
def check_desc(x):
for key in desc:
if key.lower() in x.lower():
return desc[key]
return ''
df["**Data**"] = df["Description"].map(lambda x: check_desc(x))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1376 次 |
| 最近记录: |