title: Newbie - customer profiling in Python (pandas) using loc()
body: <p>I'm a newbie, so please excuse me if I use incorrect terms. I have a df with customer purchasing info and customers are identified by a unique user_id. Each item a user_id buys in each transaction creates a new row (if a customer buys 5 products in 1 transaction, 5 different rows are created with that products info).</p>
  <p>I have created customer profiles based on 4 variables (income, age, dept id &amp; parental status) using the loc function. It has worked, however, the outcome isn't what I want. There are 106,143 customers in the df and 30,964,564 rows. The profiles I created (young parent, young single adult, higher earner, over 60, other ['other' to catch anything not assigned one of the other profiles]) are being assigned to each row, rather than to each user_id e.g. user_id 1 buys 5 items, 1 of which matches the conditions of 'young parent', the rest are assigned 'other'. This is my code:</p>
  <pre><code># create flag for 'High earner' (age: 40-59, income: 400000+, department_id: 1, 4, 7, 19, 16, parental_status: Parent)
  
  final_df.loc[(final_df['age_range'].isin(['40-49', '50-59'])) &amp; (final_df['income'] &gt;= 400000) &amp; (final_df['department_id'].isin([1, 4, 7, 19, 16])) &amp; (final_df['parental_status'] == 'Parent'), 'customer_profile'] = 'Higher earner'
  </code></pre>
  <pre><code># create flag for 'Young single adult' (age: &lt;39, income: &lt;=199999, department_id: 1, 4, 7, 19, parental_status: Non-parent)
  
  final_df.loc[(final_df['age'] &lt;= 39) &amp; (final_df['income'] &lt;= 199999) &amp; (final_df['department_id'].isin([1, 4, 7, 19])) &amp; (final_df['parental_status'] == 'Non-parent'), 'customer_profile'] = 'Young single adult'
  </code></pre>
  <pre><code># create flag for 'Young parent' (age: 20-39, income: &lt;=199999, department_id: 4, 13, 16, 17, 18 parental_status: Parent)
  
  final_df.loc[(final_df['age_range'].isin(['20-29', '30-39'])) &amp; (final_df['income'] &lt;= 199999) &amp; (final_df['department_id'].isin([4, 13, 16, 17, 18])) &amp; (final_df['parental_status'] == 'Parent'), 'customer_profile'] = 'Young parent'
  </code></pre>
  <pre><code># create flag for 'Over 60' (age: 60+, income: &lt;=199999, department_id: 1, 4, 11, 12, 15, 20 parental_status: Parent)
  
  final_df.loc[(final_df['age'] &gt;= 60) &amp; (final_df['income'] &lt;= 199999) &amp; (final_df['department_id'].isin([1, 4, 11, 12, 15, 20])) &amp; (final_df['parental_status'] == 'Parent'), 'customer_profile'] = 'Over 60'
  </code></pre>
  <pre><code># impute all NaN values as 'Other'
  
  final_df['customer_profile'].fillna('Other', inplace=True)
  </code></pre>
  <p>This is the result:</p>
  <pre><code>    user_id customer_profile
  0   1   Other
  1   1   Other
  2   1   Other
  3   1   Other
  4   1   Other
  5   1   Other
  6   1   Other
  7   1   Other
  8   1   Other
  9   1   Other
  10  1   Young parent
  11  1   Other
  12  1   Other
  13  1   Other
  14  1   Other
  15  1   Other
  16  1   Other
  17  1   Other
  18  1   Other
  19  1   Other
  20  1   Other
  21  1   Other
  22  1   Other
  23  1   Young parent
  24  1   Young parent
  </code></pre>
  <p>What I actually want is, &quot;if 'young parent' (or any profile) is assigned even once to a user_id, then all 'other' for that user_id must be changed to 'young parent' too&quot; (a customer cannot have 2 profiles!). So, the above results should show 'young parent' in each row.</p>
  <p>Is this possible? Am I using the wrong function? My knowledge is limited and any advice would be appreciated!</p>