0

df:

    Col_A     
0   011011   
1   011011   
2   011011   
3   011011   
4   011011 

How to create a column based on string position, for example in column_A I need to check 0 position create a column B.

Output;

    Col_A     Col_B
0   011011   pos1,pos4
1   000111   pos1,pos2,pos3
2   011000   pos1,pos4,pos5,pos6
3   011111   pos1
4   011010   pos1,pos4,pos6
2
  • 1
    why column A is changed in output data? Commented Jul 1, 2020 at 7:54
  • Please reformat your question. The question is hard to understand and unclear. Commented Jul 1, 2020 at 7:55

1 Answer 1

2

First convert strings to DataFrame and add columns names by function in rename:

f = lambda x: f'pos{x+1}'
df1 = pd.DataFrame([list(x) for x in df['Col_A']], index=df.index).rename(columns=f)
print (df1)
  pos1 pos2 pos3 pos4 pos5 pos6
0    0    1    1    0    1    1
1    0    0    0    1    1    1
2    0    1    1    0    0    0
3    0    1    1    1    1    1
4    0    1    1    0    1    0

Then compare '0' values by DataFrame.eq and for new column use matrix multiplication by DataFrame.dot with remove separator by Series.str.rstrip:

df['Col_B'] = df1.eq('0').dot(df1.columns + ',').str.rstrip(',')
print (df)

    Col_A                Col_B
0  011011            pos1,pos4
1  000111       pos1,pos2,pos3
2  011000  pos1,pos4,pos5,pos6
3  011111                 pos1
4  011010       pos1,pos4,pos6
Sign up to request clarification or add additional context in comments.

1 Comment

Alternate to avoid extra .str.rstrip: [','.join(df1.columns[mask]) for mask in df1.eq('0').to_numpy()]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.