我有一个字符串,bytearray(b'onefish,twofish,greenfish,bluefish\r\n1,2,3,4')
我使用该pgpy
库从加密的 csv 文件中获取该字符串。
我需要我的数据框看起来像这样。
onefish twofish greenfish bluefish
0 1 2 3 4
Run Code Online (Sandbox Code Playgroud)
到目前为止,我使用多个for
循环来创建列表,然后将其放入字典中。
如何使用以下方法来实现此目的:
df = pd.Dataframe(bytearray(b'onefish,twofish,greenfish,bluefish\r\n1,2,3,4')
获得上面我想要的输出?
>>>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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4541 次 |
最近记录: |