jeangelj
jeangelj

Reputation: 4498

Python Pandas new column based on value in one column and blank in another

I am cleaning a dataset and want to flag all values, that are incorrectly entered. For example, if a row has category A and a blank subcategory, I want to flag it.

Dataframe df:

Category | Subcategory | Value
A              aa         3635
A                         45654
B              bb         3454
C              cc         3674
C                         4575

I tried this:

df['Format_Flag'] = ' ' 
df['Format_Flag'][(df['Category'] == 'A') & (df['Subcategory'] == ' ')] = 'Y'

but it doesn't flag anything.

This is what I get:

Category |  Subcategory |   Value  | Format Flag
  A              aa         3635
  A                         45654
  B              bb         3454
  C              cc         3674
  C                         4575

This is what I am looking for:

Category |  Subcategory |   Value  | Format Flag
  A              aa         3635
  A                         45654        Y
  B              bb         3454
  C              cc         3674
  C                         4575

Upvotes: 0

Views: 2084

Answers (2)

Vaishali
Vaishali

Reputation: 38415

Using np.where

df['Format_Flag'] = np.where((df['Category'] == 'A') & (df['Subcategory'] == ' '), 'Y', ' N')

Upvotes: 2

Scott Boston
Scott Boston

Reputation: 153460

df.loc[(df_final['Category'] == 'A') & (df_final['Subcategory'] == ' '),'Format_Flag'] = 'Y'

df = df.fillna('')

Upvotes: 1

Related Questions