Home >Backend Development >Python Tutorial >How Can I Efficiently Append Rows to a Pandas DataFrame One at a Time?

How Can I Efficiently Append Rows to a Pandas DataFrame One at a Time?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-24 07:21:17700browse

How Can I Efficiently Append Rows to a Pandas DataFrame One at a Time?

Appending Rows One at a Time to a Pandas Dataframe

Creating a blank dataframe and subsequently appending rows one by one is a fundamental task for data manipulation in Pandas.

Initial Approach: Incremental Field Updates

One method for appending a single row is via incremental updates. For instance, if we have an empty dataframe with columns 'lib', 'qty1', and 'qty2', we can add a row by individually setting the values for each column:

df = pd.DataFrame(columns=('lib', 'qty1', 'qty2'))
df = df._set_value(index=len(df), col='qty1', value=10.0)

While this approach allows for selective field updates, it becomes cumbersome for bulk insertions.

Optimized Approach: Row-Based Appending

A more efficient and comprehensive method for appending rows is through row-based assignment. This involves using df.loc[i] to specify a specific row index. Here's how to implement it:

import pandas as pd
from numpy.random import randint

df = pd.DataFrame(columns=['lib', 'qty1', 'qty2'])
for i in range(5):
    df.loc[i] = ['name' + str(i)] + list(randint(10, size=2))

df

In this example, the df.loc[i] syntax assigns a row at index i with a list comprising a string and two random integers.

This approach provides a concise and efficient way to append multiple rows to a dataframe, significantly reducing the code complexity and improving efficiency.

The above is the detailed content of How Can I Efficiently Append Rows to a Pandas DataFrame One at a Time?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn