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!
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]:
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

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