在python中汇总列

时间:2017-10-27 06:03:25

标签: python django csv

我有一个文本文件。它应与column1中的字符串匹配,并使用colum2,3,4添加并将其打印在不同的文件中。所以,我的代码是:

代码

import pandas as pd

data = pd.read_csv('/Users/ankr/Desktop/samp.txt', sep=",", header=None)
data.columns = ['name','val1', 'val2', 'val3', 'val4', 'val5']
my_df = pd.DataFrame(data)
summary = my_df.groupby('name').sum()
print summary

示例输入:

abc,1,1,1,1,0

def,1,0,0,0,1

abc,1,0,0,0,1

def,0,0,0,0,1

xyz,1,1,1,1,0

预期输出

abc,2,1,1,1,1

def,1,1,1,1,2

xyz,1,1,1,1,0

Samp.txt 文件

abc,1,1,1,1,0
abc,1,1,1,1,0
Def,1,0,0,0,1
Def,1,0,0,0,1

我的输出:

  name        val1    val2  val3      val4    val5
0  abc         1       1     1         1       0
1  abc         1       1     1         1       0
2  Def         1       0     0         0       1
3  Def         1       0     0         0       1

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

import pandas as pd

data = [('abc',1,1,1,1,0),
        ('def',1,0,0,0,1),
        ('abc',1,0,0,0,1),
        ('def',0,0,0,0,1),
        ('xyz',1,1,1,1,0)]

my_df = pd.DataFrame(data)
#Sumarize by first column then sum the other columns`
summary = my_df.groupby([0]).sum()
#This line will create a cvs file named 'my_output.csv'
summary.to_csv('my_output.csv', index=True, header=False)

output at file:
abc,2,1,1,1,1
def,1,0,0,0,2
xyz,1,1,1,1,0

答案 1 :(得分:1)

我们假设您的6列是['名称',' a',' b',' c',&# 39; d',' e']您的数据框称为df

df.groupby('name').sum()

这将汇总按第一列'名称'

分组的所有a,b,c,d,e列