-2

I have a df composed of two columns like this:

    rows    A     B

    0       1     7
    1       9     11
    2       20    30
    3       32    35

I would like to create another column so that every row of column A is inserted between two rows of column B.

The output would look like this :

    rows    C

    0       7
    1       9
    2       11
    3       20
    4       30
    5       32
    6       35

Many thanks for your help

Pierrot75
  • 113
  • 6

2 Answers2

2

Take the array of columns A and B then use flatten or ravel and take values form 1st index:

df1 = pd.DataFrame(df[['A','B']].values.flatten()[1:],columns=['C']).\
                   reset_index().rename(columns={'index':'rows'}

OR:

df1 = pd.DataFrame(df[['A','B']].values.ravel()[1:],columns=['C']).\
                       reset_index().rename(columns={'index':'rows'})

print(df1)

   rows   C
0     0   7
1     1   9
2     2  11
3     3  20
4     4  30
5     5  32
6     6  35
Space Impact
  • 13,085
  • 23
  • 48
0

Use ravel with arange and DataFrame contructor:

a = df[['A','B']].values.ravel()[1:]

df1 = pd.DataFrame({'rows':np.arange(len(a)), 'C':a})
print (df1)
   rows   C
0     0   7
1     1   9
2     2  11
3     3  20
4     4  30
5     5  32
6     6  35

You can also check What is the difference between flatten and ravel functions in numpy?

Or use melt:

df1 = (df.melt(['rows'], value_name='C')
        .sort_values('rows')['C']
        .iloc[1:]
        .reset_index(drop=True)
        .rename_axis('rows')
        .reset_index())
print (df1)
   rows   C
0     0   7
1     1   9
2     2  11
3     3  20
4     4  30
5     5  32
6     6  35
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252