Splitting a List inside a Pandas DataFrame

后端 未结 4 1834
南方客
南方客 2021-02-02 01:09

I have a csv file that contains a number of columns. Using pandas, I read this csv file into a dataframe and have a datetime index and five or six other columns.

One of

4条回答
  •  我在风中等你
    2021-02-02 02:07

    Probably not the best way from performance perspective, but still, you can leverage itertools package:

    from pandas import DataFrame, Timestamp
    import itertools
    
    d = {'date': ['4/1/11', '4/2/11'], 'ts': [[Timestamp('2012-02-29 00:00:00'), Timestamp('2012-03-31 00:00:00'), Timestamp('2012-04-25 00:00:00'), Timestamp('2012-06-30 00:00:00')], [Timestamp('2014-01-31 00:00:00')]]}
    df = DataFrame(d)
    
    res = df.to_dict()
    data = []
    for x in res['date'].keys():
      data.append(itertools.izip_longest([res['date'][x]], res['ts'][x], fillvalue=res['date'][x]))
    
    new_data = list(itertools.chain.from_iterable(data))
    df2 = DataFrame(new_data, columns=['date', 'timestamp'])
    print df2
    

    Will print :

         date  timestamp
    0  4/1/11 2012-02-29
    1  4/1/11 2012-03-31
    2  4/1/11 2012-04-25
    3  4/1/11 2012-06-30
    4  4/2/11 2014-01-31
    

提交回复
热议问题