suppose we have to following pandas dataframe
asd = pd.DataFrame({'A':['a', 'b', np.nan, 'c', np.nan], 'B':['f', np.nan, 'u', 'i', np.nan]})
I want to concat the values in columns 'A' and 'B' and put a comma ',' between them and put it into a new column asd['C'] if they are both are notnull(). Otherwise return either if the other one isnull(), or return np.nan if both are null() so the final outcome for column 'C' would be
asd['C'] = ['a, f', 'b', 'u', 'c, i', np.nan]
I tried the following
def f(asd):
if asd['A'].notnull() & asd['B'].notnull():
asd['C'] = asd['A'] + ', ' + asd['B']
elif asd['A'].notnull() & asd['B'].isnull():
asd['C'] = asd['A']
elif asd['A'].isnull() & asd['B'].notnull():
asd['C'] = asd['B']
else:
asd['C'] = np.nan
return asd['C']
asd['C'] = asd.apply(f, axis=1)
but it is giving me the following error
("'str' object has no attribute 'notnull'", 'occurred at index 0')
any help is really appreciated