Pandas dataframe creating multiple rows at once via .loc
Admittedly, this is a very late answer, but I have had to deal with a similar problem and think my solution might be helpful to others as well.
After recreating your data, it is basically a two-step approach:
Recreate data:
import pandas as pddf = pd.DataFrame({'a':[10, 20], 'b':[100,200]}, index='1 2'.split())df.loc[3, 'a'] = 30
Extend the
df.index
using.reindex
:idx = list(df.index)new_rows = list(map(str, range(4, 6))) # easier extensible than new_rows = ["4", "5"]idx.extend(new_rows)df = df.reindex(index=idx)
Set the values using
.loc
:df.loc[new_rows, "a"] = [40, 50]
giving you
>>> df a b1 10.0 100.02 20.0 200.03 30.0 NaN4 40.0 NaN5 50.0 NaN
Example data
>>> data = pd.DataFrame({ 'a': [10, 6, -3, -2, 4, 12, 3, 3], 'b': [6, -3, 6, 12, 8, 11, -5, -5], 'id': [1, 1, 1, 1, 6, 2, 2, 4]})
Case 1 Note that range
can be altered to whatever it is that you desire.
>>> for i in range(10):... data.loc[i, 'a'] = 30... >>> data a b id0 30.0 6.0 1.01 30.0 -3.0 1.02 30.0 6.0 1.03 30.0 12.0 1.04 30.0 8.0 6.05 30.0 11.0 2.06 30.0 -5.0 2.07 30.0 -5.0 4.08 30.0 NaN NaN9 30.0 NaN NaN
Case 2 Here we are adding a new column to a data frame that had 8 rows to begin with. As we extend our new column c
to be of length 10 the other columns are extended with NaN
.
>>> for i in range(10):... data.loc[i, 'c'] = 30... >>> data a b id c0 10.0 6.0 1.0 30.01 6.0 -3.0 1.0 30.02 -3.0 6.0 1.0 30.03 -2.0 12.0 1.0 30.04 4.0 8.0 6.0 30.05 12.0 11.0 2.0 30.06 3.0 -5.0 2.0 30.07 3.0 -5.0 4.0 30.08 NaN NaN NaN 30.09 NaN NaN NaN 30.0