Reputation: 45
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.
Upvotes: 0
Views: 651
Reputation: 29992
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
Upvotes: 1
Reputation: 788
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))
Upvotes: 0