Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append empty rows to Dataframe in pandas

Tags:

python

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!

like image 912
Manuel Pasieka Avatar asked Jan 20 '17 12:01

Manuel Pasieka


People also ask

How do I add a row in pandas?

The concat method can be used to add rows to a Pandas Dataframe. The concat method takes an iterable of Series or Dataframe objects and concatenates them into a single Dataframe.

How do I add a row to a DataFrame list?

By using df. loc[index]=list you can append a list as a row to the DataFrame at a specified Index, In order to add at the end get the index of the last record using len(df) function. The below example adds the list ["Hyperion",27000,"60days",2000] to the end of the pandas DataFrame.

How do you add to a data frame?

The append() method appends a DataFrame-like object at the end of the current DataFrame. The append() method returns a new DataFrame object, no changes are done with the original DataFrame.

How will you add an index row or column to a DataFrame in pandas?

The pandas. concat() method can also be used to add a column to the existing DataFrame by passing axis=1. This method will return the new DataFrame as the output, including the newly added column. Using the index, the above method will concatenate the Series with the original DataFrame.


2 Answers

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.

like image 176
EdChum Avatar answered Oct 09 '22 02:10

EdChum


You can use .loc very similarly to reindex

df.loc[df.index.tolist() + missing]
like image 43
Ted Petrou Avatar answered Oct 09 '22 04:10

Ted Petrou