1
$\begingroup$

I have the below dataframe

Text                                Keywords       Type
It’s a roll-on tube                 roll-on        ball
It is barrel                        barrel         barr
An unknown shape                                   others
it’s a assembly                     assembly       assembly
it’s a sealing assembly             assembly       factory
its a roll-on double                roll-on        factory

I have first found out the keywords, and based on the keyword and its corresponding type, it should true or false

For example, when the keyword is roll-on , the type should be "ball" or "others" when the keyword is barrel, the type should be "barr" or "others"

Output

Text                                Keywords       Type         Result
It’s a roll-on tube                 roll-on        ball         True
It is barrel                        barrel         barr         True
An unknown shape                                   others       False
it’s a assembly                     assembly       assembly     True
it’s a sealing assembly             assembly       factory      True
its a roll-on double                roll-on        factory      False
$\endgroup$

1 Answer 1

0
$\begingroup$

If you know the rules, then you can code them up as you would usually do.

For e.g.

df['Keywords'] == "roll-on" & (df['Type'] == "ball" | df['Type'] == "others") => df['Result'] = True

If you have too many rules and associations to manually code, you could consider using a look-up table + merge procedure. Here you would store all the rules and associations as a table (i.e. this would include Keywords, Type, and Result), and merge Result back on to your original dataframe (by Keywords and Type).

$\endgroup$
3
  • $\begingroup$ Its showing invalid syntax, because of "=>" this. What is the right syntax for using in pandas dataframe? $\endgroup$
    – anagha s
    Jul 18, 2019 at 13:59
  • 1
    $\begingroup$ That "code" wasn't intended to be used... Just as an example of the logic you would need. You would probably need some if and elif statements, as well as correcting the conditional data selection and correcting the assignment of values. $\endgroup$
    – bradS
    Jul 18, 2019 at 17:44
  • $\begingroup$ @anaghas have you put a reproducible code in your question to make it easier for other to give you the code ?! $\endgroup$
    – user702846
    May 28, 2021 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.