pandas之DataFrame类型文件保存

1、保存为.csv文件,详细官网文档:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html
DataFrame.to_csv(path_or_buf=None, sep=’, ‘, na_rep=’’, float_format=None, columns=None, header=True, index=True, index_label=None, mode=‘w’, encoding=None, compression=None, quoting=None, quotechar=’"’, line_terminator=’\n’, chunksize=None, tupleize_cols=None, date_format=None, doublequote=True, escapechar=None, decimal=’.’)

    import pandas as pd
    import numpy as np
    data=pd.DataFrame(np.ones(16).reshape(4,4),columns=list('abcd'))
    print(data)
        a    b    c    d
    0  1.0  1.0  1.0  1.0
    1  1.0  1.0  1.0  1.0
    2  1.0  1.0  1.0  1.0
    3  1.0  1.0  1.0  1.0 
    data.to_csv('data.csv')
    print(pd.read_csv('data.csv'))
       Unnamed: 0    a    b    c    d
    0           0  1.0  1.0  1.0  1.0
    1           1  1.0  1.0  1.0  1.0
    2           2  1.0  1.0  1.0  1.0
    3           3  1.0  1.0  1.0  1.0

2、保存为.xls文件,详细官方文档:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html

    data1=pd.DataFrame(np.zeros(16).reshape(4,4),columns=list('abcd'))
    print(data1)
             a    b    c    d
    0  0.0  0.0  0.0  0.0
    1  0.0  0.0  0.0  0.0
    2  0.0  0.0  0.0  0.0
    3  0.0  0.0  0.0  0.0
    data1.to_excel('data1.xls')
    print(pd.read_excel('data1.xls'))
       a  b  c  d
    0  0  0  0  0
    1  0  0  0  0
    2  0  0  0  0
    3  0  0  0  0

你可能感兴趣的:(pandas学习)