python pandas:根据列值拆分数据框 [英] python pandas : split a data frame based on a column value

查看:430
本文介绍了python pandas:根据列值拆分数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv文件,当我读入pandas数据框时,它看起来像:

I have a csv file, when I read into pandas data frame, it looks like:

data = pd.read_csv('test1.csv')
print(data)

输出如下:

   v1  v2  v3  result
0  12  31  31       0
1  34  52   4       1
2  32   4   5       1
3   7  89   2       0


是否有一种方法可以根据结果列中的值拆分数据帧.如果结果= 0,则转到新的数据帧data_0:


Is there a way to split the data frame base on the value in the result column.I.e. If the result=0, go to a new data frame data_0:

   v1  v2  v3  result
0  12  31  31       0
1   7  89   2       0

如果结果= 1,则转到数据框data_1

and if result=1, go to a data frame data_1

   v1  v2  v3  result
0  34  52   4       1
1  32   4   5       1


有任何熊猫功能可以做到吗?还是我必须编写自己的循环函数来创建两个数据帧?非常感谢!


Is there any pandas function can do that? Or I have to write my own loop function to create two data frames? Thanks a lot!

推荐答案

熊猫允许您以非常简单的方式对数据进行切片和操作.您也可以执行与使用密钥而不是属性名称的Yakym访问相同的操作.

Pandas allow you to slice and manipulate the data in a very straightforward way. You may also do the same as Yakym accessing with the key instead of attribute name.

data_0 = data[data['result'] == 0]
data_1 = data[data['result'] == 1]

您甚至可以通过直接处理行数据来添加结果列,例如:

You can even add results columns by manipulating row data directly eg:

data['v_sum'] = data[v1] + data[v2] + data[v3]

这篇关于python pandas:根据列值拆分数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆