pandas按行写入csv文件

代码如下:

import pandas as pd


def write_csv_line_by_line():
    d = [[str(i) for i in range(10)] for j in range(10)]
    df = pd.DataFrame(d)
    # df.to_csv('res.csv', header=False)  # 不加表头
    df.columns = ['line'+str(i) for i in range(10)]
    df.to_csv('res.csv', index=False)  # 添加表头


if __name__ == '__main__':
    write_csv_line_by_line()

结果如下:

pandas按行写入csv文件_第1张图片

你可能感兴趣的:(python,pandas,python)