pandas 在循环中设置数据帧的名称
我想创建多个数据框,这些数据框的名称与该列之一中的值相同.我希望这段代码像这样工作:
I want to create multiple dataframes of names that the same as values in one of the column. I would like this code to work like that:
import pandas as pd
data=pd.read_csv('athlete_events.csv')
Sports = data.Sport.unique()
for S in Sports:
name=str(S)
name=data.loc[data['Sport']==S]
您可以通过修改 globals()
来做到这一点,但这并不是真正的建议.
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
虽然这是对您问题的直接答案,但我建议使用sacul的答案,字典是为此目的(即存储键和值)而通过 globals()
插入的变量名通常不是首先是个好主意.
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.
想象一下将来有其他人或您自己阅读代码-突然您正在使用 football
,就像以前从未明确定义过的 pd.DataFrame
-你应该怎么知道怎么回事?
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?