Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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_Dataframe - Fatal编程技术网

Python 如何按每个数据帧的长度拆分/分组数据帧列表

Python 如何按每个数据帧的长度拆分/分组数据帧列表,python,pandas,dataframe,Python,Pandas,Dataframe,例如,我有一个包含100个数据帧的列表,一些数据帧的列长度为8,其他数据帧的列长度为10,其他数据帧的列长度为12。我希望能够根据它们的列长度将它们分成组。我尝试过字典,但无法在循环中正确附加它 以前尝试过的代码: col_count = [8, 10, 12] d = dict.fromkeys(col_count, []) for df in df_lst: for i in col_count: if i == len(df.columns):

例如,我有一个包含100个数据帧的列表,一些数据帧的列长度为8,其他数据帧的列长度为10,其他数据帧的列长度为12。我希望能够根据它们的列长度将它们分成组。我尝试过字典,但无法在循环中正确附加它

以前尝试过的代码:

col_count = [8, 10, 12]

d = dict.fromkeys(col_count, [])

for df in df_lst:
    for i in col_count:
        if i == len(df.columns):
            d[i] = df

但这似乎每次都会取代dict中的值。我也试过了。append,但它似乎会附加到所有键。

而不是将
df
分配给
d[列计数]
。你应该附加它

您使用
d=dict.fromkeys(col\u count,[])初始化了d,因此
d
是空列表的字典

当您执行
d[i]=df
时,您将用数据帧替换空列表,因此
d
将成为数据帧的字典。如果您执行
d[i].append(df)
操作,您将拥有数据帧列表的字典。(这就是你想要的)


另外,我不确定您是否需要
col\u count
变量。你可以只做
d[len(df.columns)]。append(df)

我想这对你来说应该足够了。考虑如何动态地解决问题,以便更好地使用Python

In [2]: import pandas as pd

In [3]: for i in range(1, 5):
   ...:     exec(f"df{i} = pd.DataFrame(0, index=range({i}), columns=list('ABCD'))") #making my own testing list of dataframes with variable length
   ...:

In [4]: df1 #one row df
Out[4]:
   A  B  C  D
0  0  0  0  0

In [5]: df2 #two row df
Out[5]:
   A  B  C  D
0  0  0  0  0
1  0  0  0  0

In [6]: df3 #three row df
Out[6]:
   A  B  C  D
0  0  0  0  0
1  0  0  0  0
2  0  0  0  0

In [7]: L = [df1, df2, df3, df4, df5] #i assume all your dataframes are put into something like a container, which is the problem

In [13]: my_3_length_shape_dfs = [] #you need to create some sort of containers for your lengths (you can do an additional exec in the following In

In [14]: for i in L:
    ...:     if i.shape[0] == 3: #add more of these if needed, you mentioned your lengths are known [8, 10, 12]
    ...:         my_3_length_shape_dfs.append(i) #adding the df to a specified container, thus grouping any dfs that are of row length/shape equal to 3
    ...:         print(i)
    ...:
   A  B  C  D
0  0  0  0  0
1  0  0  0  0
2  0  0  0  0

您尝试的代码是什么?这不是拆分,而是grouping@VaidøtasI。我的意思是把清单分开。是的,它还对数据帧进行分组。不管怎样,你能帮忙吗?或者你只是在这里发表评论?..@ygorg我已经用更多的信息更新了帖子:)@我仍然有一个答案给你。如果它回答了你的问题,那么+1并接受它:)你能详细说明我将如何使用列表吗?@我仍然更新了我的答案,希望它更清晰。不要犹豫在代码中的任何地方放置
打印
,看看会发生什么。当我将d[I]=df更改为d[I].append(df)时,数据帧会附加到字典中的所有键,我不知道为什么。