David Y.
—I would like to create an empty Python Pandas DataFrame and add rows to it one by one. How can I achieve this?
While it is possible to add rows to a DataFrame after it has been created, this approach has several downsides compared to the standard practice of creating a new DataFrame from a list or dictionary. It is slower and more memory intensive, the datatypes for elements in new rows will not be automatically inferred, and numeric labels may not behave as desired. Per the Pandas documentation:
It is not recommended to build DataFrames by adding single rows in a for loop.
If we want to build up data for a DataFrame iteratively, we should work with either dictionaries or lists until we’re ready to create the final DataFrame. For example:
import pandas # Make a 5x5 list of lists data = [] for x in range(5): data.append([]) for y in range(5): data[x].append(y) # Create a DataFrame from the list of lists df = pandas.DataFrame(data) print(df) # will print # 0 1 2 3 4 # 0 0 1 2 3 4 # 1 0 1 2 3 4 # 2 0 1 2 3 4 # 3 0 1 2 3 4 # 4 0 1 2 3 4
In some instances, this may be insufficient for our needs. Older versions of Pandas provided a DataFrame.append
method, but this has been deprecated in favor of pandas.concat
. We can use this latter method to add rows to an existing DataFrame:
import pandas # Make a 5x5 list of lists data = [] for x in range(5): data.append([]) for y in range(5): data[x].append(y) # Create a DataFrame from the list of lists df = pandas.DataFrame(data) # Create a new row and add it to the DataFrame new_row = pandas.DataFrame([[0, 1, 2, 3, 4]]) df = pandas.concat([df, new_row]) print(df) # will print # 0 1 2 3 4 # 0 0 1 2 3 4 # 1 0 1 2 3 4 # 2 0 1 2 3 4 # 3 0 1 2 3 4 # 4 0 1 2 3 4 # 0 0 1 2 3 4
As we can see, the new row’s label is 0
rather than 5
. We can fix this by renaming the row:
df.index = df.index[:-1].tolist() + [5] # remove the final row label and add a new label print(df) # will print # 0 1 2 3 4 # 0 0 1 2 3 4 # 1 0 1 2 3 4 # 2 0 1 2 3 4 # 3 0 1 2 3 4 # 4 0 1 2 3 4 # 5 0 1 2 3 4
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.