Add data to DataFrame by function

Question:

thats my function:

import pandas as pd


shopping_list = pd.DataFrame()
shopping_list = shopping_list.assign(Order=0, Type=0, Price=0, Quantity=0)


def add_item(order: str, type_transaction: str, price: float, quantity: int, shopping_list=shopping_list):
    new_item_data = pd.DataFrame({"Order": [order],
                                  "Type": [type_transaction],
                                  "Price": [price],
                                  "Quantity": [quantity]})
    return pd.concat([shopping_list, new_item_data], ignore_index=True)


add_item(order="Buy", type_transaction="Add", price=20.0, quantity=100)
add_item(order="Sell", type_transaction="Add", price=25.0, quantity=200)

print(shopping_list)

Output:

Empty DataFrame
Columns: [Order, Type, Price, Quantity]
Index: []

What i should do to add this items into my dataframe ? bcz they vanish and idk why

Asked By: Robson

||

Answers:

Here with some changes to make things work:

import pandas as pd

shopping_list = pd.DataFrame()
shopping_list = shopping_list.assign(Order=0, Type=0, Price=0, Quantity=0)


def add_item(order: str, type_transaction: str, price: float, quantity: int, shopping_list=shopping_list):
    new_item_data = pd.DataFrame({"Order": [order],
                                  "Type": [type_transaction],
                                  "Price": [price],
                                  "Quantity": [quantity]})
    return pd.concat([shopping_list, new_item_data], ignore_index=True)

shopping_list = add_item(order="Buy", type_transaction="Add", price=20.0, quantity=100, shopping_list=shopping_list)
shopping_list = add_item(order="Sell", type_transaction="Add", price=25.0, quantity=200, shopping_list=shopping_list)

print(shopping_list)

Firstly, pd.concat returns a concatenation of the data frames you pass in, but it leaves the inputs untouched. So, you need to assign what you return fomr add_item to a variable.

Secondly, default arguments for python functions are only evaluated once when the function is defined. That means that your default for shopping_list will always be an empty list, no matter what the variable shopping_list contains.

Answered By: Daniel Schneider

You can improve the performance of the operation by generating a list of dictionary objects and performing a single call to pd.concat(). Appending data on a row-by-row basis is never performant.

import pandas as pd

data = [{"Order": "Buy", "Type": "Add", "Price": 20.0, "Quantity": 100},
        {"Order": "Sell", "Type": "Add", "Price": 25.0, "Quantity": 200}]

shopping_list = pd.DataFrame()
shopping_list = shopping_list.assign(Order=0, Type=0, Price=0, Quantity=0)
shopping_list = pd.concat([shopping_list, pd.DataFrame(data)], ignore_index=True)
Answered By: Dan Nagle
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.