[英]How to divide a pandas dataframe into smaller dataframes, based on a column value?
I want my dataframe to get splitted into smaller dfs, based on 'z' value. 我希望基于'z'值将我的数据帧拆分为更小的dfs。 In this case, 2 dfs as I only want to take whats between the zeros (z column). 在这种情况下,2 dfs,因为我只想在零(z列)之间取得什么。 ie Dataframe1: 01/10/2018 0:30 - 1/10/2018 1:20 AND Dataframe2: 01/10/2018 2:00 - 1/10/2018 2:40 即Dataframe1:01/10/2018 0:30 - 1/10/2018 1:20 AND Dataframe2:01/10/2018 2:00 - 1/10/2018 2:40
How can this be done in a loop for bigger datasets? 如何在更大的数据集循环中完成此操作? Discarding the zeroes and only putting whats in between. 丢弃零,只介绍两者之间的什么。
Here, I am having a sample dataset with two columns and few sample rows. 在这里,我有一个包含两列和几个样本行的样本数据集。 I have splitted this dataframe into three new dataframes based on a condition (col2 divisible by 3 and arrange them as per their remainder values). 我已根据条件将此数据帧拆分为三个新数据帧(col2可被3整除,并根据其余值排列)。
from datetime import datetime, timedelta
import numpy as np
import pandas as pd
data = pd.DataFrame({'Col1':np.arange(datetime(2018,1,1),datetime(2018,1,12),timedelta(days=1)).astype(datetime),'Col2':np.arange(1,12,1)})
print('Data:')
print(data)
# split dataframe into three dataframes based on the col2 divisible by 3
# col2 % 3 == 0 then data_0
# col2 % 3 == 1 then data_1
# col2 % 3 == 2 then data_2
data_0, data_1, data_2 = data[data['Col2']%3==0], data[data['Col2']%3==1],data[data['Col2']%3==2]
print('Data_0:')
print(data_0)
print('Data_1:')
print(data_1)
print('Data_2:')
print(data_2)
The generated output is as: 生成的输出如下:
Data:
Col1 Col2
0 2018-01-01 1
1 2018-01-02 2
2 2018-01-03 3
3 2018-01-04 4
4 2018-01-05 5
5 2018-01-06 6
6 2018-01-07 7
7 2018-01-08 8
8 2018-01-09 9
9 2018-01-10 10
10 2018-01-11 11
Data_0:
Col1 Col2
2 2018-01-03 3
5 2018-01-06 6
8 2018-01-09 9
Data_1:
Col1 Col2
0 2018-01-01 1
3 2018-01-04 4
6 2018-01-07 7
9 2018-01-10 10
Data_2:
Col1 Col2
1 2018-01-02 2
4 2018-01-05 5
7 2018-01-08 8
10 2018-01-11 11
Hope, this may helps you. 希望,这可能会对你有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.