0

I have been given a list called data which has the following content

data=[b'Name,Age,Occupation,Salary\r\nRam,37,Plumber,1769\r\nMohan,49,Elecrician,3974\r\nRahim,39,Teacher,4559\r\n']

I wanted to have a pandas dataframe which looks like the link Expected Dataframe

How can I achieve this.

2 Answers 2

1

You can try this:

data=[b'Name,Age,Occupation,Salary\r\nRam,37,Plumber,1769\r\nMohan,49,Elecrician,3974\r\nRahim,39,Teacher,4559\r\n']

processed_data = [x.split(',') for x in data[0].decode().replace('\r', '').strip().split('\n')]
df = pd.DataFrame(columns=processed_data[0], data=processed_data[1:])

Hope it helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks This worked perfectly. Will it have any performance issue if used over a large data? This example was a simple one. Actual use case may have millions of records.
I think it should be fine (may take a few seconds at max). List comprehensions in python are very fast. But let me know if it slows down too much.
Please consider accepting the answer if it solved your problem. :)
0

I would recommend you to convert this list to string as there is only one index in this list

str1 = ''.join(data)

Then use solution provided here

import sys
if sys.version_info[0] < 3: 
    from StringIO import StringIO
else:
    from io import StringIO

import pandas as pd

TESTDATA = StringIO(str1)
df = pd.read_csv(TESTDATA, sep=",")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.