How to use a Dataframe content as function input?

Question:

Evening Mates,

I want to know if is there anyway of using the a table content as input for a function without having to get each individual value.

Like unpacking a table as we do for tuples. Kinda function(*(tuple)) but instead of using a tuple, i want to use a single row of a dataframe, where each column will be used as a sequential input.

What i’m trying to do is:

# Table that has a single row with name and age
table_content = table.loc[table['ID'] == id]

# I want to cast it like this
function_for_name_and_age(table_content)

# Without having to do
table_content_name = table_content['Name'].values(0)
table_content_age = table_content['Age'].values(0)

function_for_name_and_age(table_content_name, table_content_age )

'''
Asked By: culpinha

||

Answers:

You can convert a df row to a tuple.

import pandas as pd


table = pd.DataFrame([[1, "Tom", 36, 3], [2, "Bob", 30, 2]], columns=["ID", "Name", "Age", "Children"])
table_content = table.loc[table["ID"] == 1]

_id, name, age, children = table_content.to_records(index=False)[0]


def function_for_name_and_age(table_name: str, table_age: int) -> tuple:
    return table_name, table_age


print(function_for_name_and_age(name, age))
Answered By: snake_charmer_775
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.