pradeepsf729 / pandas_101

Pandas questions with solutions
1 stars 0 forks source link

isin #16

Closed sandeepny441 closed 9 months ago

sandeepny441 commented 1 year ago

import pandas as pd

data = { 'Store_ID': [101, 102, 103, 104, 105, 106, 107, 108, 109, 110], 'Store_Name': ['Walmart', 'Target', 'Costco', 'Aldi', 'Trader Joe\'s', 'Safeway', 'Kroger', 'Walmart', 'Whole Foods', 'Sprouts'], 'City': ['Los Angeles', 'San Francisco', 'San Diego', 'San Jose', 'Sacramento', 'Fresno', 'Oakland', 'Los Angeles', 'Santa Clara', 'Pasadena'], 'Annual_Revenue_Millions': [450, 320, 480, 190, 240, 260, 210, 400, 310, 150], 'Category': ['Supermarket', 'Department', 'Warehouse', 'Supermarket', 'Supermarket', 'Supermarket', 'Supermarket', 'Supermarket', 'Organic', 'Organic'] }

stores_df = pd.DataFrame(data)

  1. How would you filter the DataFrame to get stores located in either 'Los Angeles' or 'San Diego'?
  2. Can you use isin to find out which stores in the DataFrame have an annual revenue of either 450 or 480 million?
  3. How would you use isin to identify rows where the store name is either 'Walmart' or 'Aldi'?
  4. Imagine you have a list selected_cities = ['Sacramento', 'Oakland']. How would you use the isin method to filter the DataFrame based on this list?
  5. Can you identify which rows have the store category either as 'Organic' or 'Warehouse'?
  6. How would you use the isin method to exclude rows where the store name is 'Trader Joe's'?
  7. If you had a Series s = pd.Series([101, 103, 105]), how would you use isin to filter rows in the DataFrame with matching Store_IDs?
  8. How can you combine isin with the ~ operator to filter out rows where the city is either 'San Francisco' or 'Santa Clara'?
  9. Can you find out which stores are not categorized as 'Supermarket' using the isin method?
  10. Imagine there's an external list of store names for a promotion. How would you use the isin method to filter only the rows of the DataFrame that match the store names in this list?
pradeepsf729 commented 1 year ago

How can you combine isin with the ~ operator to filter out rows where the city is either 'San Francisco' or 'Santa Clara'?

It is either or neither?, for either we dont need "~"

sandeepny441 commented 1 year ago

yeah it should have been neither i guess