Home > other >  How to use a Dataframe content as function input?
How to use a Dataframe content as function input?

Time:08-15

Evening Mates,

I want to know if is there anyway of using the a table content as input for a funcion 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 sequencial 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 )

'''

CodePudding user response:

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))
  • Related