python的多文件统计及表头的添加,有问题,仅供参考

#coding=utf-8 


#多个文件统计并重新添加表头
#第一个没问题,之后的统计有问题
import pandas as pd
import os
import glob
import codecs
import pandas as pd
import os
import glob
import codecs
import csv
from collections import Counter
  
#dd=[]  
# use glob to get all the csv files 
# in the folder
#path = os.getcwd()


#path = r"F://良好数据的csv文件"
#path = r"D://Desktop"
#csv_files = glob.glob(os.path.join(path, "*.csv"))
  
file_name = []
data=[]
data1=[]
dd=[]
dd1=[]
keys = ['0','1','2','3','8','11','12','13','14','15','16','17','18','Flase','Ture']
 

#for f in csv_files:
#    path=f.split("\\")[-1]
#    file_name.append(path)
 
f=r'D://Desktop//301.csv'
#h=f[(f['rec_code']=='14')&(f['serverity']=='3')]
#print(file_name)


with open(f,encoding='utf-8') as csv_file:
    writer_obj = csv.DictReader(csv_file)
    
    countNormal = 0
    countUnoraml = 0
    for row in writer_obj:
        #data.append(row['type_id'])
#        data.append(row['rec_code'])
        #data1.append((f['rec_code']==14)&(f['severity']==3))
        #data1.append(f['rec_code'==14&'serverity'==3])
        #正确的       
 #       data.append((row['rec_code']!='14')&(row['severity']=='3'))
        if(row['rec_code']!='14')&(row['severity']=='3'):
            countUnormal = countUnoraml +1
        else:
           countNormal = countNormal + 1

    data.append([countUnormal, countNormal])


    #统计异常的个数

from collections import Counter
res = Counter(data)#res 为字典
#res1 = Counter(data1)#res 为字典
dd.append(res)
#dd1.append(res1)

#f1=f[f['rec_code']!=14&['severity']!=3]
print(dd)
#print(dd1)

#name=['0','1']

name2=[file_name]

test=pd.DataFrame(columns=keys,data=dd)

#test['axis_1']=test.loc[:,['11','12','13','14','15','16','18']].apply(lambda x:x.sum(),axis=1)
test['axis_1']=test.loc[:,['11','12','13','14','15','16','18']].apply(lambda x:x.sum(),axis=1)
test['axis_2']=test.loc[:,['0','1','2','3','8','17']].apply(lambda x:x.sum(),axis=1)

通过以上的函数来计算表中数据的和添加到后面的并写到下面


print(test)

#test.to_csv("d:/测试csv文件/1.csv",encoding="gbk")
#test.to_csv("C:/Users/1.csv",encoding="gbk")
test.to_csv("d:/测试csv文件/3.csv",encoding="gbk")


df=pd.read_csv('1.csv',header=None,names=['文件名','未知','单','正孔','接头','正缝','头伤','头垂直劈裂','伤头','异孔','腰伤损','底伤损','波消失','异缝','异常','非异常'])#,'异常','非异常'
df.to_csv('1.csv',index=False,encoding='utf-8-sig')

重新将列名的写入


 

你可能感兴趣的:(每日学习,python)