【发布时间】:2019-03-23 13:12:06
【问题描述】:
我有一个脚本,它从元素的主列表中删除“坏元素”,然后返回一个包含更新元素及其关联值的 csv。
我的问题是,是否有更有效的方法在 for 循环中执行相同的操作?
Master=pd.read_csv('some.csv', sep=',',header=0,error_bad_lines=False)
MasterList = Master['Elem'].tolist()
MasterListStrain1 = Master['Max_Principal_Strain'].tolist()
#this file should contain elements that are slated for deletion
BadElem=pd.read_csv('delete_me_elements_column.csv', sep=',',header=None, error_bad_lines=False)
BadElemList = BadElem[0].tolist()
NewMasterList = (list(set(MasterList) - set(BadElemList)))
filename = 'NewOutput.csv'
outfile = open(filename,'w')
#pdb.set_trace()
for i,j in enumerate(NewMasterList):
#pdb.set_trace()
Elem_Loc = MasterList.index(j)
line ='\n%s,%.25f'%(j,MasterListStrain1[Elem_Loc])
outfile.write(line)
print ("\n The new output file will be named: " + filename)
outfile.close()
【问题讨论】:
-
如果你仍然在使用 pandas,有一个
pd.to_csv函数。
标签: python large-data