I have a df:
c1 c2 c3
A None A
B A None
C None None
None None None
I am trying to add a column which contains the column names 'c1-c3' if the respective columns are not null
So the resulting df should be:
c1 c2 c3 c4
A None A c1|c3
B A None c1|c2
C None None c1
None None None None
Constructor
data = {'c1': ['A', 'B', 'C', None],
'c2': [None, 'A', None, None],
'c3:':['A',None,None,None]}
df = pd.DataFrame(data)
CodePudding user response:
So in your case do dot
df['new'] = df.notna().dot(df.columns '|').str[:-1]
Out[151]:
0 c1|c3
1 c1|c2
2 c1
3
dtype: object
CodePudding user response:
I think this will do what you're asking:
import pandas as pd
data = {'c1': ['A', 'B', 'C', None],
'c2': [None, 'A', None, None],
'c3':['A',None,None,None]}
df = pd.DataFrame(data)
df['c4'] = df.apply(lambda x: [df.columns[i] for i in range(len(df.columns)) if x[df.columns[i]] is not None], axis=1).str.join('|')
df.loc[df['c4'] == '', 'c4'] = None
print(df)
Output:
c1 c2 c3 c4
0 A None A c1|c3
1 B A None c1|c2
2 C None None c1
3 None None None None
CodePudding user response:
Not sure if it is the most elegant way, but it will work:
import re
def new_col(df):
col_value =''
if df['c1'] is None:
col_value = df_t.columns[0]
if df['c2'] is None:
col_value =col_value '|' df_t.columns[1]
if df['c3'] is None:
col_value =col_value '|' df_t.columns[2]
col_value = re.sub('^\|','',col_value )
return col_value
df['c4'] = df.apply(new_col,axis = 1)