Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert in every second row in pandas? [duplicate]

Basically, I have a DataFrame which looks like this:

  c1 c2 
0  a  b  
1  c  d  
2  e  f  
3  g  h  

I need to convert it to this one:

  c1 
0  a   
1  b   
2  c   
3  d
4  e
...

I know how to get all the values from the second column:

second_col_items = [df[['1']].iloc[i].item() for i in range(0,len(df.index))] 

But I'm stuck on inserting. I need to insert rows in loop, and, moreover, I need to insert new rows between the existing ones. Is it even possible?

So, my question is: how to iterate through the list (second_col_items in my case) and insert it's values to every second row in DataFrame? Thanks in advance!

like image 550
7duck Avatar asked Oct 26 '25 07:10

7duck


2 Answers

you can use stack() method:

source DF

In [2]: df
Out[2]:
  c1 c2
0  a  b
1  c  d
2  e  f
3  g  h

stacked

In [3]: df.stack()
Out[3]:
0  c1    a
   c2    b
1  c1    c
   c2    d
2  c1    e
   c2    f
3  c1    g
   c2    h
dtype: object

stacked + reset_index

In [4]: df.stack().reset_index(drop=True)
Out[4]:
0    a
1    b
2    c
3    d
4    e
5    f
6    g
7    h
dtype: object

In [5]:
like image 175
MaxU - stop WAR against UA Avatar answered Oct 28 '25 01:10

MaxU - stop WAR against UA


You can unwind with ravel or flatten. Both are numpy methods that can be applied the the values attribute of a pd.DataFrame or pd.Series

solution

pd.Series(df.values.ravel(), name='c1')

0    a
1    b
2    c
3    d
4    e
5    f
6    g
7    h
Name: c1, dtype: object

Or

pd.DataFrame(dict(c1=df.values.ravel())

  c1
0  a
1  b
2  c
3  d
4  e
5  f
6  g
7  h

naive time test

enter image description here

like image 31
piRSquared Avatar answered Oct 27 '25 23:10

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!