I this dataframe:
Receipt Description Card Member Account Cost
200a apple adam 08203928 $2
20022a pear bob 08203228 $7
202a orange alice 0820321228 $8
I want to check if a value in the description
column contains a specific substring. For example the first row (adam) has the description of "apple". I want to check to see if the substring "appl" exists in this description
column.
If so I then want to create a new column named Data
which would then store the value need more apples
. If no substring of "appl" is found I dont want to store anything in this column.
This is what the expected new dataframe would look like.
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
You can try this:
Example 1:
df["**Data**"] = df["Description"].map(lambda x: "apple containes" if "appl" in x else '')
Example 2
If you have mapping of every fruit to check then you could create like this
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))
contains a string and np.where() to check if it contains a string. I do.
df['**Data**'] = np.where(df['Description'].str.contains('apple'),'need more apples','')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.