I have the column in my dataframe containing list of strings of vary length as shown below:
names venue
[Instagrammable, Restaurants, Vegan] 14 Hills
[Date Night, Vibes, Drinks] Upper 14
[Date Night, Drinks, After Work Drinks, Cocktail] Hills
. .
. .
.
Now if i want to check that if some list is present in my dataframe or not, How to do it.
Example1:
Input :
find_list=[Date Night, Vibes, Drinks]
venue = 'Upper 14'
Output:
Record is present in my dataframe
Example 2:
Input :
find_list=[Date Night, Drinks]
venue='Hills 123'
Output:
Record is not present in my dataframe
Example
Input :
find_list=[ Date Night, Vibes, Drinks]
venue = 'Upper 14'
Output:
Record is not present in my dataframe
You can use .apply()
and .any()
:
find_list = ["Date Night", "Vibes", "Drinks"]
if df["names"].apply(lambda x: x == find_list).any():
print("List is present in my dataframe")
else:
print("List is not present in my dataframe")
Prints:
List is present in my dataframe
EDIT: To match a record:
find_list = ["Date Night", "Vibes", "Drinks"]
venue = "Upper 14"
if df.apply(
lambda x: x["names"] == find_list and x["venue"] == venue, axis=1
).any():
print("Record is present in my dataframe")
else:
print("Record is not present in my dataframe")
Prints:
Record is present in my dataframe
EDIT 2: To strip whitespaces from input list:
find_list = [" Date Night", "Vibes", "Drinks"]
venue = "Upper 14"
if df.apply(
lambda x: all(a.strip() == b.strip() for a, b in zip(x["names"], find_list))
and x["venue"] == venue,
axis=1,
).any():
print("Record is present in my dataframe")
else:
print("Record is not present in my dataframe")
Prints:
Record is present in my dataframe
EDIT 3: To remove extra spaces between words:
import re
find_list = [" Date Night", "Vibes", "Drinks"]
venue = "Upper 14"
r = re.compile(r"\s{2,}")
if df.apply(
lambda x: all(
r.sub(a.strip(), " ") == r.sub(b.strip(), " ")
for a, b in zip(x["names"], find_list)
)
and x["venue"] == venue,
axis=1,
).any():
print("Record is present in my dataframe")
else:
print("Record is not present in my dataframe")
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.