0

Hi is there a way to filter out unique values ina a pandas data frame. I am using the code below to filter out the unique values. However, I am getting different ordered combinations. For example, ['Creative, Modern Cuisine', 'Modern Cuisine, Creative'] is there a way to filter this out.

[Part of the data]

cuisine = df.Cuisine.unique()
cuisine_count = df.Cuisine.nunique()
print(cuisine, cuisine_count)
5
  • cuisine = cuisine.drop_duplicates() Commented Apr 22, 2022 at 22:28
  • Though, it sounds like the real issue is in how your dataframe is formatted, can you provide a couple sample lines from it? Commented Apr 22, 2022 at 22:30
  • @BeRT2me yea this is part of the data set I am working on Cuisine Creative, Modern Cuisine Creative Creative Creative Modern Cuisine, Creative Classic French Classic French, Creative Modern French, Creative Classic French Creative Creative Classic French French Modern Cuisine Modern British Modern French French Creative, Contemporary Taizhou Vegetarian Cantonese French Contemporary I am try to only get the unique values Commented Apr 22, 2022 at 22:33
  • 1
    How to create a Minimal, Reproducible Example Commented Apr 22, 2022 at 22:36
  • Hi sorry, @BeRT2me. I uploaded a part of the dataframe Commented Apr 22, 2022 at 22:41

1 Answer 1

1

If I understand your intent, you are trying to get a list of all distinct cuisines which appear in your DataFrame. Try this:

df['Cuisine'].str.split(',').explode().str.strip().unique().tolist()

Explanation:

  • df['Cuisine'].str.split(','): split Cuisine strings at commas, producing a Series with a Python list in each row, where each list item holds an individual cuisine string
  • .explode(): for each list of cuisine strings, transform each string to a row
  • .str.strip(): strip whitespace
  • .unique().tolist(): get list of unique cuisines
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.