python pandas : split a data frame based on a column value

后端 未结 3 526
一生所求
一生所求 2021-01-06 17:03

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

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

output looks like:

3条回答
  •  旧巷少年郎
    2021-01-06 17:32

    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]
    

提交回复
热议问题