ari6739
ari6739

Reputation: 91

list of Bytes object to dataframe

I have a list of Bytes (strings) which is separated by "\n". I want to create a data frame from the list, and to separate each element to 15 columns. I have succeeded to separate the rows but I'm getting 1 column instead of 15.

from io import BytesIO
df = pd.read_csv(BytesIO(b'\n'.join(tmp)), sep = '\n')
df

my output looks like this: enter image description here

row example:
b"US\t20422322\tR8MEA6IGAHO0B\tB00MC4CED8\t217304173\tBlackVue DR600GW-PMP\tMobile_Electronics\t5\t0\t0\tN\tY\tVery Happy!\tAs advertised. Everything works perfectly, I'm very happy with the camera. As a matter of fact I'm going to buy another one for my 2nd car.\t2015-08-31\n" b'\n'

Upvotes: 0

Views: 387

Answers (1)

d.hatch75
d.hatch75

Reputation: 60

From your output it looks like your tmp list contains \t characters already, which would imply that the \t character is the separator in the data and not \n as you have specified. It is unlikely that \n is a separator between columns, which is what the sep argument of read_csv() refers to, so I think you may be getting confused with the \n you're using as a separator between rows.

Try instead to do the following:

df = pd.read_csv(BytesIO(b'\n'.join(tmp)), sep='\t')

Upvotes: 1

Related Questions