Python3 pd.dataframe写入csv和xlsx

Python3 pd.dataframe写入csv和xlsx。代码如下:


df = pd.DataFrame(
    columns={'dist','distance','409','425', '442', '458', '474', '491', '507', '523', '540', '556','572', '589', '605', '621', '637', '653', '670', '686', '703', '719', '735', '751', '768', '784','800', '816', '833', '840', '865', '882', '898', '914'})
for ...

    df.loc[count] = {'dist':j , 'distance': dist, '409': array32[0], '425': array32[1], '442': array32[2],\
                     '458': array32[3],'474': array32[4], '491': array32[5], '507': array32[6], '523': array32[7],\
                         '540': array32[8],'556': array32[9],'572': array32[10], '589': array32[11], '605': array32[12],\
                             '621': array32[13],'637': array32[14],'653': array32[15],'670': array32[16], '686': array32[17],\
                                 '703': array32[18], '719': array32[19],'735': array32[20],'751': array32[21],'768': array32[22],\
                                     '784': array32[23], '800': array32[24],'816': array32[25],'833': array32[26],'840': array32[27],\
                                         '865': array32[28], '882': array32[29],'898': array32[30], '914': array32[31]}


#写入csv
df.to_csv(output_dir + '\\' +specie_name+str(i+1)+'_merged.csv',index=False)
#写入xlsx
df.to_excel(output_dir + '\\' +specie_name+str(i+1)+'_merged.xlsx',index=False)

如报错:No module named ‘openpyxl’
则pip install openpyxl安装一下此包。

你可能感兴趣的:(Python,python,csv,xlsx,pd.dataframe读写)