9

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!

3 Answers 3

11

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.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use .loc very similarly to reindex

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

Comments

0

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

2 Comments

Why is name 'missing' is not defined
@sikisis because you didn't define it. See OP...it has it defined as missing = ['d', 'e']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.