Dima Lituiev Dima Lituiev - 2 months ago 18
Python Question

pandas: write tab-separated dataframe with literal tabs with no quotes

I have to reformat my data for a genetics software which requires to split each column into two, e.g

0-> G G; 1-> A G; 2 -> A A;
. The output file is supposed to be tab-delimited. I am trying to do it in pandas:

import csv
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0,3, size = (10,5)),
columns=[ chr(c) for c in range(97, 97+5) ])

def fake_alleles(x):
if x==0:
return "A\tA"
if x==1:
return "A\tG"
if x==2:
return "G\tG"

plinkpast6 = df.applymap(fake_alleles)
plinkpast6.to_csv("test.ped", sep="\t", quoting=csv.QUOTE_NONE)


Which gives me an error
Error: need to escape, but no escapechar set
. Are there other ways to do it with
pandas
?

Answer

sep="\t" is trying to take each element of the dataframe row and insert a "\t" in between. Problem is there are "\t" in the elements and it's confusing it. It wants you to escape those "\t"s in the elements and you haven't. I suspect you want your final output to be 6 columns.

Try this:

import csv
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0,3, size = (10,20)))

def fake_alleles(x):
    if x==0:
        return "A\tA"
    if x==1:
        return "A\tG"
    if x==2:
        return "G\tG"

plinkpast6 = df.iloc[:,:3].applymap(fake_alleles)
plinkpast6 = plinkpast6.stack().str.split('\t', expand=True).unstack()
plinkpast6.to_csv("test.ped", sep="\t", quoting=csv.QUOTE_NONE)
Comments