[英]How can I split a DataFrame into multiple DataFrames of fewer rows?
If I have a DataFrame
that is composed of 100 rows w/ 4 columns for sake of example, how can I create 5 new DataFrames
that are each composed of 20 rows w/ 4 columns?例如,如果我有一个由 100 行和 4 列组成的DataFrame
,我如何创建 5 个新的DataFrames
,每个数据帧由 20 行和 4 列组成?
That is, if an arbitrary column of the original DataFrame
holds the list [0, 1, 2, 3, ..., 98, 99]
, how would I create 5 new DataFrames
such that the first DataFrame
's arbitrary column holds the list [0, 1, 2, ..., 9]
, the second DataFrame
's arbitrary column holds the list [10, 11, 12, ..., 19]
, etc. etc.?也就是说,如果原始DataFrame
的任意列包含列表[0, 1, 2, 3, ..., 98, 99]
,我将如何创建 5 个新的DataFrames
,以便第一个DataFrame
的任意列包含列表[0, 1, 2, ..., 9]
,第二个DataFrame
的任意列包含列表[10, 11, 12, ..., 19]
等等?
I tried the following to a DataFrame
consisting of a single column A
that holds the list [0, 1, 2, 3, ..., 98, 99]
, but it gave me 100 CSV
files each w/ a single row rather than the desired 5 CS enter code here
V files each w/ 20 rows:我尝试对DataFrame
进行以下操作,其中包含包含列表[0, 1, 2, 3, ..., 98, 99]
的单列A
,但它给了我 100 个CSV
文件,每个文件只有一行,而不是所需的 5 CS enter code here
V 文件,每个文件有 20 行:
import pandas as pd
import numpy as np
list = []
for i in range (0, 100):
list.append(i)
df = pd.DataFrame(data=list, columns=['A'])
groups = df['A'].groupby(np.arange(len(df['A']/10)))
for (frameno, frame) in groups:
frame.to_csv("/Users/ephemeralhappiness/Desktop/Cycle Test/" + "%s.csv" % frameno)
Just change your groupby to:只需将您的 groupby 更改为:
# to get 5 groups
nrows = 20
groups = df.groupby(df.index // nrows)
print(groups.ngroups)
5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.