Using a list of columns with boolean indexing

Issue

From my analysis I have discovered that Disloyal 30-40 year old customers are Not Satisfied with Company X. "Not Satisfied" means they have rated services and products 0-2 out of a possible 5. I want to know what inputs were ranked <=2.

I stored the columns in a list to use in a for loop so I could index the relevant column values which are rankings 0-5.

What is the syntax for using the column variable in the boolean expression?

Example Data:

Customer Type    Age    Satisfaction    Design   Food    Wi-Fi    Service    Distance
     Disloyal     28   Not Satisfied         0      1        2          2        13.5
        Loyal     30       Satisfied         5      3        5          4        34.2
     Disloyal     36   Not Satisfied         2      0        2          4        55.8

Code

ranked_cols = ['Design', 'Food', 'Wi-Fi', 'Service', 'Distance']

for column in df[ranked_cols]:
    columnSeriesObj = df[column]

sub = df[
(df["Customer Type"] == "Disloyal")
& (df["Satisfaction"] == "Not Satisfied")
& df["Age"].between(30, 40)
]

sub[(sub[ranked_cols] <= 2)].shape[0]

(sub.melt(value_vars=[c for c in sub.columns if c.startswith(column)])
.groupby("variable")
.value_counts()
.to_frame()
.reset_index()
.rename(columns={0: "count"}))

Solution

Try this:

# Choose the cols you want to see the ratings for
ranked_cols = [
    "Design",
    "Food",
    "Wi-Fi",
    "Service",
]

# Select the relevant customers
sub = df[
    (df["Customer Type"] == "Disloyal")
    & (df["Satisfaction"] == "Not Satisfied")
    & df["Age"].between(30, 40)
]

(
    sub.melt(value_vars=ranked_cols)
    .groupby("variable")
    .value_counts()
    .to_frame()
    .reset_index()
    .rename(columns={"value": "rating", 0: "count"})
)

This will output a DataFrame contaning all the ranked_cols categories and their respective rating and how many times that rating was given (count):

    variable  rating  count
0   Design    2       1
1   Food      0       1 
2   Service   4       1
3   Wi-Fi     2       1

Answered By – 965311532

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published