Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Add Rows to DataFrame

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?

like image 369
Hexagon789 Avatar asked Oct 17 '25 12:10

Hexagon789


1 Answers

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.

like image 146
Cleb Avatar answered Oct 20 '25 02:10

Cleb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!