1

enter image description here

a=data["Col2"] a=a.dropna() a=pd.DataFrame(a) k=[] for i in data["Col1"]: k.append(i) o=1 for i in a.index: o=o+i k.insert(o,a['COLOR2'][i]) o+=1 

But the problem here is the index of Col1 changes after 1st insertion. And after adding 1(o+=1), it still does not give the desired output.

1
  • There is multiple columns? Ouput is one column DataFrame ? Commented Sep 16, 2021 at 5:40

2 Answers 2

1

If need new one column DataFrame from columns Col1, Col2 use:

df = data[['Col1','Col2']].stack().reset_index(drop=True).to_frame('Col') 
Sign up to request clarification or add additional context in comments.

Comments

0

Use a counter initialized to 1, such as -

a=data["Col2"] a=a.dropna() a=pd.DataFrame(a) k=[] for i in data["Col1"]: k.append(i) counter=1 for i in a.index: o=i+counter k.insert(o,a['Col2'][i]) counter+=1 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.