简体   繁体   English

每隔 n 行切片 Pandas DataFrame

[英]Slicing Pandas DataFrame every nth row

I have a CSV file, which I read in as a pandas DataFrame.我有一个 CSV 文件,我将其作为 pandas DataFrame 读入。 I want to slice the data after every nth row and store the it as an individual CSV.我想在每第 n 行之后对数据进行切片并将其存储为单独的 CSV。

My data looks somewhat like this:我的数据看起来有点像这样:

index,acce_x,acce_y,acce_z,grav_x,grav_y,grav_z
0,-2.53406373,6.92596131,4.499464420000001,-2.8623820449999995,7.850541115,5.129520459999999
1,-2.3442032099999994,6.878311170000001,5.546690349999999,-2.6456542850000004,7.58022081,5.62603916
2,-1.8804458600000005,6.775125179999999,6.566146829999999,-2.336306185,7.321197125,6.088656729999999
3,-1.7059021099999998,6.650866649999999,7.07060242,-2.1012737650000006,7.1111130000000005,6.416324900000001
4,-1.6802886999999995,6.699703990000001,7.15823367,-1.938001715,6.976289879999999,6.613534820000001
5,-1.6156433,6.871610960000001,7.13333286,-1.81060772,6.901037819999999,6.72789553
6,-1.67286072,7.005918899999999,7.22047422,-1.722352455,6.848503825,6.8044359100000005
7,-1.56608278,7.136883599999999,7.150566069999999,-1.647941205,6.821055315,6.850329440000001
8,-1.3831649899999998,7.2735946999999985,6.88074028,-1.578703155,6.821634375,6.866061665000001
9,-1.25986478,7.379898050000001,6.590330490000001,-1.5190086099999998,6.839881785,6.861375744999999
10,-1.1101097050000002,7.48500525,6.287461959999999,-1.4641099750000002,6.870566649999999,6.842625039999999

For example, I would like to slice it after every 5th row and store the rows indexed 1-4 and 5-9 each in a single CSV (so in this case I would get 2 new CSVs), row 10 should be discarded.例如,我想在每 5 行之后对其进行切片,并将索引为 1-4 和 5-9 的行分别存储在一个 CSV 中(所以在这种情况下我会得到 2 个新的 CSV),第 10 行应该被丢弃。 One issue is that I'll have to apply this to multiple files which differ in length as well as naming the newly created CSVs.一个问题是我必须将其应用于长度不同的多个文件以及命名新创建的 CSV。

You can do it with a for loop:您可以使用for循环来做到这一点:

for i in range(round(len(df)/5)): #This ensures all rows are captured
   df.loc[i*5:(i+1)*5,:].to_csv('Stored_files_'+str(i)+'.csv')

So the first iteration it'll be rows 0 to 5 stored with name "Stored_files_0.csv The second iteration rows 5 to 10 with name "Stored_files_1.csv" And so on...所以第一次迭代它将是第 0 到 5 行,名称为“Stored_files_0.csv”第二次迭代第 5 到 10 行,名称为“Stored_files_1.csv”等等......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM