Writing dataframe to csv WITHOUT removing commas

前端 未结 3 1707
礼貌的吻别
礼貌的吻别 2021-01-21 05:57

I want to write a pandas dataframe to csv. One of the columns of the df has entries which are lists, e.g. [1, 2], [3, 4], ...

When I use df.to_csv(\'output.csv\') and I

相关标签:
3条回答
  • 2021-01-21 06:30

    df.to_csv('output.tsv', sep='\t')

    Will separate the values with tabs instead of commas.

    .tsv is tab separated value file

    0 讨论(0)
  • 2021-01-21 06:40

    Add keyword argument quoting=csv.QUOTE_ALL:

    import csv
    import pandas as pd
    
    data = {"A": [1,2,3], "B": [[11,12],[21,22],[31,32]]}
    df = pd.DataFrame(data)
    df.index.name='index'
    
    # df
    #        A         B
    # index             
    # 0      1  [11, 12]
    # 1      2  [21, 22]
    # 2      3  [31, 32]
    
    df.to_csv('test.csv',quoting=csv.QUOTE_ALL)
    

    Output:

    "index","A","B"
    "0","1","[11, 12]"
    "1","2","[21, 22]"
    "2","3","[31, 32]"
    
    0 讨论(0)
  • 2021-01-21 06:43

    I needed commas in my csv file, too. So, I tried to get it using df but in vain. I decided to do the following. I read tap-separated text file, first, and write to a csv file with commas added. Not sure this is what you exactly want, but this way you can have commas in your csv file. Hope it helps.

    with open('test.txt', newline = '') as cos:
        co_reader = csv.reader(cos, delimiter='\t')
        with open('output.csv', 'w', newline='') as csvfile:
            cowriter = csv.writer(csvfile, delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL)
        for co in cog_reader:
            cowriter.writerow(co)
    
    0 讨论(0)
提交回复
热议问题