0

I need to assign "correct" to a new column"A" based on other column"B" condition , that is:- if column "B" has non blank entry then column "A" will assign value of "Correct" else "check required" condition we have: if column "B" contains any non blank entry then column "A" will assign with "correct" input:- Name B A Adam 34
Screen
binny 35
tough
sunny 5
petter

output:- Name B A 0 Adam 34.0 Correct 1 Screen Correct 2 binny 35.0 Correct 3 tough Correct 4 sunny 5.0 Correct 5 petter Correct

I have tried the following:

df.loc[df['A'] != '', 'B'] = "Correct"

input enter image description here

output enter image description here

MD SAJID
  • 106
  • 1
  • 8
  • 1
    Possible duplicate of [pandas create new column based on values from other columns](https://stackoverflow.com/questions/26886653/pandas-create-new-column-based-on-values-from-other-columns) – razdi Apr 18 '19 at 04:43
  • Take a look at this question: https://stackoverflow.com/questions/26886653/pandas-create-new-column-based-on-values-from-other-columns – razdi Apr 18 '19 at 04:44
  • i seen that example before,it is some different what i am facing actually , i have to apply condition when exiting column not equal to blank, means if exiting column contains any value then , new column will assign with "correct". – MD SAJID Apr 18 '19 at 05:16
  • provide sample data and expected output in your question – Sociopath Apr 18 '19 at 05:21
  • I guess you need `df.loc[df['A'].notna(), 'B'] = "Correct"` - if empty value is missing value – jezrael Apr 18 '19 at 05:39

2 Answers2

0
def label_A(row):
  if row['B'].notna():
    return "Correct"
  else:
    return "Check Required"

df['A'] = df.apply(lambda row: label_A(row), axis=1)

This is based on the answer given in: pandas create new column based on values from other columns

The function label_A() checks if the value of column B is empty or no and then assigns the appropriate value to column A.

razdi
  • 1,388
  • 15
  • 21
  • i tried it... but actually it assigning "Correct" for blank entry as well non blank entries..i am sharing input details & output details – MD SAJID Apr 18 '19 at 05:52
  • I edited it slightly. Try the notna() instead of != '' – razdi Apr 18 '19 at 05:55
  • Not working... with above code.. finally this code working for correct condition df.loc[df['B'].notna(), 'A'] = "Correct" – MD SAJID Apr 18 '19 at 06:05
0

finally this code working df.loc[df['B'].notna(), 'A'] = "Correct"

MD SAJID
  • 106
  • 1
  • 8