Search code examples
pythonpandaspgp

How to convert string that uses "\r\n" as line breaks to pandas dataframe


I have a string bytearray(b'onefish,twofish,greenfish,bluefish\r\n1,2,3,4') I get this string from an encrypted csv file using the pgpy library.

I need my dataframe to look like this.

      onefish twofish greenfish bluefish
0     1       2       3         4

So far I use multiple for loops that make lists that I then put into a dictionary.

How can approach this using: df = pd.Dataframe(bytearray(b'onefish,twofish,greenfish,bluefish\r\n1,2,3,4') to get the output I want above?


Solution

    • from given data. Split data by '\r\n'.
    • assuming First row is always header pass that as columns and rest of the rows as data.
    >>>import pandas as pd
    >>>b = b'onefish,twofish,greenfish,bluefish\r\n1,2,3,4\r\n5,6,7,8' 
    >>>data = list(map(lambda x: x.split(','),b.decode('utf-8').split("\r\n")))
    >>>pd.DataFrame(data[1:], columns=data[0])
    
        onefish     twofish     greenfish   bluefish
    0      1          2            3           4
    1      5          6            7           8