Read CSV into a dataFrame with varying row lengths using Pandas

前端 未结 6 1744
孤城傲影
孤城傲影 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:25

    add extra columns (empty or otherwise) to the top of your csv file. Pandas will takes the first row as the default size, and anything below it will have NaN values. Example:

    file.csv:

    a,b,c,d,e
    1,2,3
    3
    2,3,4
    

    code:

    >>> import pandas as pd
    >>> pd.read_csv('file.csv')
       a    b    c   d   e
    0  1  2.0  3.0 NaN NaN
    1  3  NaN  NaN NaN NaN
    2  2  3.0  4.0 NaN NaN
    

提交回复
热议问题