How do I determine if the values in each column are included in the text?
import pandas as pd
df = pd.DataFrame(
data = {
'Context': ['Clear in Tokyo', 'Cloudy Kanagawa', 'Hot Okinawa Climate',
Keywords: ['Fukuoka', 'Kanagawa', 'Okinawa']},
index=['xx', 'yy', 'zz']
)
display(df)
#How do I determine if the values in each column are included in the text?
# Do I have to loop?I hope the pd.Series.str.contains() argument is Series.
print('◇Expected Results')
expired_df = pd.DataFrame(
data = {
'Context': ['Clear in Tokyo', 'Cloudy Kanagawa', 'Hot Okinawa Climate',
Keywords: ['Fukuoka', 'Kanagawa', 'Okinawa',
'Flag': False, True, True
},
index=['xx', 'yy', 'zz']
)
display(expanded_df)
It's almost like a loop, but you can write it like this
df['flag'] = df.apply(lambdas:s['keyword']ins['context'],axis=1)
display(df)
© 2024 OneMinuteCode. All rights reserved.