Manuel Pasieka
Manuel Pasieka

Reputation: 151

Append empty rows to Dataframe in pandas

I want to append empty rows (filled with np.NaN) to a pandas dataframe and currently only know how to do this using loc

T = pd.DataFrame(index=['a', 'b', 'c'], data={'Col0': 0, 'Col1': 1})
T
   Col0  Col1
a     0     1
b     0     1
c     0     1

missing = ['d', 'e']
for m in missing:
    T.loc[m] = np.NaN

  Col0  Col1
a   0.0   1.0
b   0.0   1.0
c   0.0   1.0
d   NaN   NaN
e   NaN   NaN

Do you know of a more elegant way to do this?

Why it is not possible to do something like

T.loc[missing] = np.NaN

thx!

Upvotes: 9

Views: 14195

Answers (3)

piRSquared
piRSquared

Reputation: 294218

If you actually have a dataframe, you can use pd.concat

df = pd.DataFrame(index=missing)

pd.concat([T, df])

Or alternatively, you can use pd.DataFrame.append

df = pd.DataFrame(index=missing)

T.append(df)

Both yield:

   Col0  Col1
a   0.0   1.0
b   0.0   1.0
c   0.0   1.0
d   NaN   NaN
e   NaN   NaN

Upvotes: 0

Ted Petrou
Ted Petrou

Reputation: 61947

You can use .loc very similarly to reindex

df.loc[df.index.tolist() + missing]

Upvotes: 2

EdChum
EdChum

Reputation: 393963

You can reindex by taking the union of the current index and the missing row values:

In [281]:    
T.reindex(T.index.union(missing))

Out[281]:
   Col0  Col1
a   0.0   1.0
b   0.0   1.0
c   0.0   1.0
d   NaN   NaN
e   NaN   NaN

basically loc looks for the passed in labels, unfortunately setting with enlargement only works for a single row label.

It'd be more efficient to do the above, here we take the union of the current index and the missing values and use these to reindex the df, where rows don't exist NaN values are inserted.

Upvotes: 11

Related Questions