How do I write data to csv file in columns and rows from a list in python?

前端 未结 5 1592
独厮守ぢ
独厮守ぢ 2020-12-15 07:40

everyone.I have a list of lists and I want to write them in a csv file with columns and rows.I have tried the writerows but it isn\'t what I want.An example of my list is th

5条回答
  •  不思量自难忘°
    2020-12-15 07:58

    import pandas as pd
    header=['a','b','v']
    df=pd.DataFrame(columns=header)
    for i in range(len(doc_list)):
      d_id=(test_data.filenames[i]).split('\\')
      doc_id.append(d_id[len(d_id)-1])
      df['a']=doc_id
    print(df.head())
    df[column_names_to_be_updated]=np.asanyarray(data)
    print(df.head())
    df.to_csv('output.csv')
    

    Using pandas dataframe,we can write to csv. First create a dataframe as per the your needs for storing in csv. Then create csv of the dataframe using pd.DataFrame.to_csv() API.

提交回复
热议问题