Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过迭代创建单独的数据帧_Python_Pandas - Fatal编程技术网

Python 通过迭代创建单独的数据帧

Python 通过迭代创建单独的数据帧,python,pandas,Python,Pandas,我有一个类似这样的df: Year Month Day 1984 01 01 1984 02 01 1985 01 01 1985 02 01 1986 01 01 1986 02 01 我想根据年份和月份创建单独的数据框架。因此,每个年==1984和月==01都有自己的数据帧。每个Year==1985和month==01都有自己的数据框架,所以对于年和月的每一个可能组合,我都需要一个新的数据框架。这是一个简单的例子,因为我的年龄

我有一个类似这样的df:

Year  Month  Day
1984  01     01
1984  02     01
1985  01     01
1985  02     01
1986  01     01
1986  02     01

我想根据年份和月份创建单独的数据框架。因此,每个
年==1984
月==01
都有自己的数据帧。每个
Year==1985
month==01
都有自己的数据框架,所以对于年和月的每一个可能组合,我都需要一个新的数据框架。这是一个简单的例子,因为我的年龄从1984年到2011年,每个月都在变化。我知道我可以使用
.isin
手动执行此操作,但如果可能的话,我希望将其自动化。我希望这是有意义的。

您可以使用嵌套列表

dfs = {year: {month: pd.DataFrame() for month in range(1, 13)} 
       for year in df.Year.unique()}
这将返回一个嵌套字典,键入年份和月份,其中包含一个空数据框(您没有指定希望数据框包含的内容…)