Collapsing categories in a csv python

Viewed 108

I have a dataframe 'locations' which contains the genre of some stores, and it is very cluttered with lots of different categories, so I want to combine some of the categories so there are less and more simple categories. How do I do this?

Example:

  store        type
 mcdonalds     fast-food
 nandos        sit-down-food
 wetherspoons  tech-pub
 southsider    pub-and-dine

Id like to combine categories fast-food and sit-down-food to become just 'food', and tech-pub and pub-and-dine to become just 'pub'. How do I do this?

2 Answers

My first instinct would be to use the pandas apply function to map the values as desired. Something along the lines of:

import pandas as pd

def nameMapper(name):
    if "food" in name:
        return "food"
    elif "pub" in name:
        return "pub"
    else:
        return "something else"


data = [
     ["mcdonalds", "fast-food"], 
     ["nandos","sit-down-food"],
     ["wetherspoons","tech-pub"],
     ["southsider","pub-and-dine"]
     ]

df = pd.DataFrame(data, columns={"store", "type"})
print(df)

print("---------------------------")

df["type"] = df["type"].apply(nameMapper)
print(df)

When I ran this the following output was produced

$ python3 answer.py 
          store           type
0     mcdonalds      fast-food
1        nandos  sit-down-food
2  wetherspoons       tech-pub
3    southsider   pub-and-dine
---------------------------
          store  type
0     mcdonalds  food
1        nandos  food
2  wetherspoons   pub
3    southsider   pub

You can use a dict keyed by the types you want to replace with the desired types as the values. Then set the column to a list comprehension that replaces the types but keeps the ones you want.

# Dict specifying the types to replace
type_dict = {'fast-food':'food','sit-down-food':'food',
             'tech-pub':'pub','pub-and-dine':'pub'}
# Replace types that are dict keys but keep the values that aren't dict keys
df['type'] = [type_dict.get(i,i) for i in df['type']]
Related