python读取文件操作.CSV

时间:2024-09-01 12:37:02
#-*- encoding:utf-8 -*-
import numpy as np
import pandas as pd def test(): # header=0,表示文件第0行为列索引
# index_col=0,表示文件第0列为行索引 # typeTable=pd.read_csv('./data/type.csv',header=None)
userRatingTable=pd.read_csv('./data/RatingTable.csv',header=None) userRatingTable.index=range(0,943)#行号
userRatingTable.columns=range(0,1682)#列号 i=0#i表示文件号
# print(userTable[1])#取第1列,#注意::这里前后颠倒了,表示矩阵中用户10对项目4的评分,即评分为4
for j in range(943):
# print(userRatingTable.ix[942])#取第一行
temp=userRatingTable.ix[j] userTable=pd.read_csv('./data/preprefe_%s.csv'%str(i),header=0,index_col=0)
userTable.index=range(0,1682)
userTable.columns=range(0,19) simMat=[]
Userscore=[]
count=0
for k in range(1682):
# print(temp[k])
# print(type(temp[k]))
if temp[k]!=0:
# print(temp[k])
count=count+1
# print(temp[k])
# print(userTable.ix[k])
Userscore.append(temp[k])
simMat.append(userTable.ix[k])
# print(simMat)
data_to_excel=pd.DataFrame(simMat,index=np.arange(0,count),columns=np.arange(0,19))
data_to_excel.to_csv('./data/data1/preprefeggg_%s.csv'%str(i))
print(Userscore,count)
simMat=[]
Userscore=[]
count=0 # if i==3:
# exit()
i=i+1 if __name__=='__main__':
test()