I am attempting to create 3 dataframes, and name them after the 2nd element of each list.
In my actual project, there are 10 dataframes, and the number could vary.
I don't know how to structure the return statement in the function. I also believe that when I state x[1] = pd.DataFrame()
, I am inadvertently attempting to change the list value, whereas my intention is to use the list value as the df name itself.
import pandas as pd
names = [
['SPtab', 'sp', 'man'],
['SBtab', 'sb', 'man'],
['SDtab', 'sd', 'man']
]
def create_dfs():
for x in names:
x[1] = pd.DataFrame()
return
print(sp)
print(sb)
print(sd)
CodePudding user response:
This should work (using dynamic global variables is a bad practice and it would be better if you use a python dictionary, this solution uses dynamic variables. The lower one uses a dictionary):
import pandas as pd
names = [
['SPtab', 'sp', 'man'],
['SBtab', 'sb', 'man'],
['SDtab', 'sd', 'man']
]
def create_dfs():
for x in names:
exec(f"%s = pd.DataFrame()" % (x[1]), globals())
create_dfs()
create_dfs()
creates several empty variables with the names defined in names
. It uses the built in exec()
function to define a variable with the name defined in the names
list. When exec()
is called, it is called with globals()
as a parameter to make sure that the variable is a global variable.
Or you could do it with a dictionary:
import pandas as pd
names = [
['SPtab', 'sp', 'man'],
['SBtab', 'sb', 'man'],
['SDtab', 'sd', 'man']
]
def create_dfs(names):
dfs = {}
for x in names:
dfs[x[1]] = pd.DataFrame()
return dfs
dfs = create_dfs(names)
For this solution create_dfs()
returns a dictionary containing all of the dataframes. This output is assigned to a variable called dfs