Let's say I have an empty dataframe, already set up with columns, but no rows. I'm scraping some data from the web so let's say I need to add an index '2176'
to the empty dataframe. How could I automatically add this row to the database when I try to assign it? Is this even pandas purpose or should I use something else?
As an alternative to .loc
, you might want to consider at
. Using @NickBraunagel's example:
df = pd.DataFrame(columns=['foo1','foo2'])
Then
df.at['2716', 'foo1'] = 10
yields
foo1 foo2
2716 10 NaN
Timings are quite different:
# @NickBraunagel's solution
%timeit df.loc['2716', 'foo1'] = 10
1000 loops, best of 3: 212 µs per loop
# the at solution
%timeit df.at['2716', 'foo1'] = 10
100000 loops, best of 3: 12.5 µs per loop
If you want to add several column entries at the same time, you can do:
d = {'foo1': 20, 'foo2': 10}
df.at['1234', :] = d
yielding
foo1 foo2
2716 10 NaN
1234 20 10
However, make sure to always add the same datatype to avoid errors or other undesired effects as explained here.
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