One - One Code All

Blog Content

pandas库pd.to_csv操作写入数据与自带csv库写入csv数据比较

Python 统计学-科学计算   2018-08-23 22:26:19

先说结论,自带的csv写入比pandas快一倍左右。


pandas库pd.to_csv操作写入数据与自带csv库写入csv数据

100 rows
pd.to_csv: 0.002480030059814453
csv.writerow: 0.0004699230194091797

10000 rows
pd.to_csv: 0.03421902656555176
csv.writerow: 0.013949871063232422
import time
import pandas as pd


def pd_tocsv():
    output_file = 'csv_test_pd.csv'
    
    list1 = []
    list2 = []
    for i in range(0, 10000):
        list1.append("abcd")
        list2.append(0.198773)
    
    data = {'ticker': list1, 'value': list2}
    data_df = pd.DataFrame(data)
    data_df.to_csv(output_file)


def csv_tocsv():
    output_file = 'csv_test_sys.csv'
    with open(output_file, 'w', newline='') as f:
        csv_write = csv.writer(f)
        list_header = ['ticker', 'value']
        csv_write.writerow(list_header)

        for i in range(0, 10000):
            list_tmp = ['abcd', 0.1987654]
            csv_write.writerow(list_tmp)


starttime = time.time()
#pd_tocsv()
csv_tocsv()
endtime1 = time.time()
print(endtime1 - starttime)



上一篇:bert安装和demo
下一篇:h5py和python多核编程mpi4py的使用

The minute you think of giving up, think of the reason why you held on so long.