计算CSV文件中特定列中的重复值,并将值返回到另一列(python2)

时间:2017-04-25 05:21:05

标签: python csv python-2.x

我目前正在尝试计算CSV文件列中的重复值,并将值返回到python中的另一个CSV列。

例如,我的CSV文件:

KeyID    GeneralID
145258   KL456
145259   BG486
145260   HJ789
145261   KL456

我想要实现的是计算有多少数据具有相同的GeneralID并将其插入到新的CSV列中。例如,

KeyID    Total_GeneralID
145258   2
145259   1
145260   1
145261   2

我尝试使用拆分方法拆分每个列,但它没有那么好用。

我的代码:

case_id_list_data = []

with open(file_path_1, "rU") as g:
    for line in g:
        case_id_list_data.append(line.split('\t'))
        #print case_id_list_data[0][0] #the result is dissatisfying 
        #I'm stuck here.. 

5 个答案:

答案 0 :(得分:3)

您必须分三步完成任务:  1.读取CSV文件  2.生成新列的值  3.将值添加到文件中     导入csv     import fileinput     import sys

# 1. Read CSV file
# This is opening CSV and reading value from it.
with open("dev.csv") as filein:
    reader = csv.reader(filein, skipinitialspace = True)
    xs, ys = zip(*reader)

result=["Total_GeneralID"]

# 2. Generate new column's value
# This loop is for counting the "GeneralID" element.
for i in range(1,len(ys),1):
    result.append(ys.count(ys[i]))

# 3. Add value to the file back
# This loop is for writing new column
for ind,line in enumerate(fileinput.input("dev.csv",inplace=True)):
    sys.stdout.write("{} {}, {}\n".format("",line.rstrip(),result[ind]))

我还没有使用临时文件或任何高级模块,如熊猫或任何东西。

答案 1 :(得分:2)

import pandas as pd
#read your csv to a dataframe
df = pd.read_csv('file_path_1')
#generate the Total_GeneralID by counting the values in the GeneralID column and extract the occurrance for the current row.
df['Total_GeneralID'] = df.GeneralID.apply(lambda x: df.GeneralID.value_counts()[x])
df = df[['KeyID','Total_GeneralID']]
Out[442]: 
    KeyID  Total_GeneralID
0  145258                2
1  145259                1
2  145260                1
3  145261                2

答案 2 :(得分:2)

您可以使用pandas库:

import pandas as pd

df = pd.read_csv('file')
s = df['GeneralID'].value_counts().rename('Total_GeneralID')
df = df.join(s, on='GeneralID')
print (df)
    KeyID GeneralID  Total_GeneralID
0  145258     KL456                2
1  145259     BG486                1
2  145260     HJ789                1
3  145261     KL456                2

答案 3 :(得分:2)

如果你对熊猫不利并希望继续使用标准库:

<强>代码:

import csv
from collections import Counter
with open('file1', 'rU') as f:
    reader = csv.reader(f, delimiter='\t')
    header = next(reader)
    lines = [line for line in reader]
    counts = Counter([l[1] for l in lines])

new_lines = [l + [str(counts[l[1]])] for l in lines]
with open('file2', 'wb') as f:
    writer = csv.writer(f, delimiter='\t')
    writer.writerow(header + ['Total_GeneralID'])
    writer.writerows(new_lines)

<强>结果:

KeyID   GeneralID   Total_GeneralID
145258  KL456   2
145259  BG486   1
145260  HJ789   1
145261  KL456   2

答案 4 :(得分:0)

使用csv.reader而不是split()方法。 它更容易。

由于