简体   繁体   English

如何根据列值将pandas数据帧划分为更小的数据帧?

[英]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. 希望,这可能会对你有所帮助。

You can use groupby for that. 您可以使用groupby

grouped = df.groupby('z')    
dataframes = [grouped.get_group(x) for x in grouped.groups]#list of DataFrames

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

相关问题 如何根据一列中的唯一值将熊猫数据帧划分为不同的数据帧并对其进行迭代? - how to divide pandas dataframe into different dataframes based on unique values from one column and itterate over that? 如何将 pandas dataframe 划分为多个较小的数据帧或元组列表? - How do i divide a pandas dataframe into multiple smaller dataframes or lists of tuples? 根据一列python将数据框拆分为较小的数据框 - Split a dataframe into smaller dataframes based on a column python Python Pandas Dataframe:根据单独的列取下一个较小的值 - Python Pandas Dataframe: Take next smaller value based on separate column 根据列的值将 Pandas dataframe 拆分为多个数据帧 - Split a Pandas dataframe into multiple dataframes based on the value of a column Pandas 根据列值将 Dataframe 划分为另一个 - Pandas Divide Dataframe by Another Based on Column Values 如何划分基于数据帧的第一列? - How to divide the DataFrames based first column? 按顺序将大的 dataframe 分成较小的子数据帧 - Divide a large dataframe into smaller sub dataframes in order 根据空行将Pandas数据框拆分为多个较小的数据框 - Split a Pandas Dataframe into multiple smaller dataframes based on empty rows 如何将前 4 个列表值划分为数据框熊猫中的列 - How to divide first 4 list value into column in dataframe pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM