Determine if the values in each column are included in the text

Asked 1 years ago, Updated 1 years ago, 283 views

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)

Enter a description of the image here

python python3 pandas

2023-02-15 05:49

1 Answers

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)


2023-02-15 15:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.