0

Trying to create a function to specify the name of a DataFrame, so it can be used within another function. Tried a few different approaches, but this is a simplified version of what I have now.

import pandas as pd

data1 = {'var1': [1, 2, 3], 'var2': [1, 2, 3]}
h_df1 = pd.DataFrame(data1)


def hh(wave):
    hh = 'h_' + wave
    return hh


def table(wave):
    hh_data = hh(wave)
    hh_data["new_var"] = hh_data["var1"]*hh_data["var2"]
    return table


table = table("df1")

print(table)

This gives the error: TypeError: string indices must be integers

The result I am expecting is that hh_data uses a DataFrame named h_df1. Looking for a way to automate which DataFrame is picked up. It's part of a more complex system.

1
  • hh_data is just string, not dict. Commented Apr 6, 2021 at 10:18

2 Answers 2

1

I think you want is Python built-in globals functions, which returns a dictionary representing the current global symbol table. For example, globals()["h_df1"] will return the h_df1 variable. Thus, you can do

def table(wave):
    hh_data = globals()[hh(wave)]
    hh_data["new_var"] = hh_data["var1"]*hh_data["var2"]
    return table
Sign up to request clarification or add additional context in comments.

2 Comments

When I do this using separate files for each function, I get the error: KeyError: 'h_df1'
@an1uk Well, From the doc, This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called). globals() doesn't work with separate files.
0
  • hh_data is just string, not dict.
  • if you want to create new column, you can try below code
if __name__ == '__main__':
    import pandas as pd

    data1 = {'var1': [1, 2, 3], 'var2': [1, 2, 3]}
    h_df1 = pd.DataFrame(data1)

    h_df1['new_var'] = h_df1["var1"] * h_df1["var2"]

    print(h_df1.head(5))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.