Read CSV into a dataFrame with varying row lengths using Pandas

前端 未结 6 1743
孤城傲影
孤城傲影 2020-12-03 22:32

So I have a CSV that looks a bit like this:

1 | 01-01-2019 | 724
2 | 01-01-2019 | 233 | 436
3 | 01-01-2019 | 345
4 | 01-01-2019 | 803 | 933 | 943 | 923 | 954         


        
6条回答
  •  自闭症患者
    2020-12-03 23:23

    If using only pandas, read in lines, deal with the separator after.

    import pandas as pd
    
    df = pd.read_csv('data.csv', header=None, sep='\n')
    df = df[0].str.split('\s\|\s', expand=True)
    
       0           1    2     3     4     5     6
    0  1  01-01-2019  724  None  None  None  None
    1  2  01-01-2019  233   436  None  None  None
    2  3  01-01-2019  345  None  None  None  None
    3  4  01-01-2019  803   933   943   923   954
    4  5  01-01-2019  454  None  None  None  None
    

提交回复
热议问题