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!
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.
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.
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.
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.
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.
You can use .loc
very similarly to reindex
df.loc[df.index.tolist() + missing]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With