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!
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.
你可以使用.loc
非常类似于reindex
df.loc[df.index.tolist() + missing]
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.