6

Given the following data frame:

import pandas as pd
import numpy as np
df1=pd.DataFrame({'A':['a','b','c','d'],
                 'B':['d',np.nan,'c','f']})
df1
    A   B
0   a   d
1   b   NaN
2   c   c
3   d   f

I'd like to insert blank rows before each row. The desired result is:

    A   B
0   NaN NaN
1   a   d
2   NaN NaN
3   b   NaN
4   NaN NaN
5   c   c
6   NaN NaN
7   d   f

In reality, I have many rows.

Thanks in advance!

piRSquared
  • 285,575
  • 57
  • 475
  • 624
Dance Party
  • 3,459
  • 10
  • 42
  • 67

3 Answers3

7

I think you could change your index like @bananafish did and then use reindex:

df1.index = range(1, 2*len(df1)+1, 2)
df2 = df1.reindex(index=range(2*len(df1)))

In [29]: df2
Out[29]:
     A    B
0  NaN  NaN
1    a    d
2  NaN  NaN
3    b  NaN
4  NaN  NaN
5    c    c
6  NaN  NaN
7    d    f
Anton Protopopov
  • 30,354
  • 12
  • 88
  • 93
3

Use numpy and pd.DataFrame

def pir(df):
    nans = np.where(np.empty_like(df.values), np.nan, np.nan)
    data = np.hstack([nans, df.values]).reshape(-1, df.shape[1])
    return pd.DataFrame(data, columns=df.columns)

pir(df1)

enter image description here

Testing and Comparison

Code

def banana(df):
    df1 = df.set_index(np.arange(1, 2*len(df)+1, 2))
    df2 = pd.DataFrame(index=range(0, 2*len(df1), 2), columns=df1.columns)
    return pd.concat([df1, df2]).sort_index()

def anton(df):
    df = df.set_index(np.arange(1, 2*len(df)+1, 2))
    return df.reindex(index=range(2*len(df)))

def pir(df):
    nans = np.where(np.empty_like(df.values), np.nan, np.nan)
    data = np.hstack([nans, df.values]).reshape(-1, df.shape[1])
    return pd.DataFrame(data, columns=df.columns)

Results

pd.concat([f(df1) for f in [banana, anton, pir]],
          axis=1, keys=['banana', 'anton', 'pir'])

enter image description here

Timing

enter image description here

piRSquared
  • 285,575
  • 57
  • 475
  • 624
2

A bit roundabout but this works:

df1.index = range(1, 2*len(df1)+1, 2)
df2 = pd.DataFrame(index=range(0, 2*len(df1), 2), columns=df1.columns)
df3 = pd.concat([df1, df2]).sort()
bananafish
  • 2,877
  • 20
  • 29