You can do this by modifying globals()
but that's not really adviseable.
for S in Sports:
globals()[str(S)] = data.loc[data['Sport']==S]
Below is a self-contained example:
In [1]: import pandas as pd
In [2]: df = pd.DataFrame({'sport':['football', 'football', 'tennis'],
'value':[1, 2, 3]})
In [3]: df
Out[3]:
sport value
0 football 1
1 football 2
2 tennis 3
In [4]: for name in df.sport.unique():
...: globals()[name] = df.loc[df.sport == name]
...:
In [4]: football
Out[4]:
sport value
0 football 1
1 football 2
While this is a direct answer to your question, I would recommend sacul's answer, dictionaries are meant for this (i.e. storing keys and values) and variable names inserted via globals()
are usually not a good idea to begin with.
Imagine someone else or yourself in the future reading your code - all of a sudden you are using football
like a pd.DataFrame
which you have never explicitly defined before - how are you supposed to know what is going on?