When using pandas to save data can be saved in two forms
First, the amount of data is not very large files, can be placed in the list, a one-time storage.
Second, for large amounts of data, consider generation side, while storing, avoid open up a lot of memory space to store data destined for the list.
I Caishuxueqian, only know some of the surface of things, if wrong, please correct me hope!
Is illustrated by the following codes
. 1 Import PANDAS AS PD 2 . 3 . 4 class SaveCsv: . 5 . 6 DEF the __init__ (Self): . 7 self.clist = [[l, 2,3], [4,5,6], [7,8,9 ]] . 8 . 9 DEF saveFile (Self, my_list): 10 "" " . 11 the documents saved as csv file format, header write column name, index name write line 12 is : param my_list: a list of data to be stored 13 is : return: 14 "" " 15 DF = pd.DataFrame (Data = [my_list]) 16 df.to_csv ( " ./Test.csv " , encoding ="utf-8-sig", mode="a", header=False, index=False) 17 18 def saveAll(self): 19 """ 20 一次性存储完 21 :return: 22 """ 23 pf = pd.DataFrame(data=self.clist) 24 pf.to_csv("./Test_1.csv", encoding="utf-8-sig", header=False, index=False) 25 26 27 def main(self): 28 nameList = ["Beijing " , " Shanghai " , " Guangzhou " , " Shenzhen " , " xiongan " , " MAGAZINE " ] 29 # Start represents the cycle starts counting at 30 for NUM, Data in the enumerate (nameList, Start = 1 ): 31 is IF NUM == 2% 0: 32 self.savefile ([ " success " , Data, NUM]) 33 is the else : 34 is self.savefile(["失败", data, num]) 35 return 0 36 37 if __name__ == '__main__': 38 sc = SaveCsv() 39 sc.main() 40 sc.saveAll()
The following code is briefly described:
savefile: can be saved while circulating through to the same file, the meanings of some parameters can be self Baidu
saveAll: through a one-time storage, all data stored by the end of the list once