lundi 3 avril 2017

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_final['Category'] == 'A') & (df_final['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

Aucun commentaire:

Enregistrer un commentaire