将列表元素合并到Pandas中的一个数据框列表中

时间:2017-11-06 10:01:27

标签: python pandas

我从csv文件读取数据帧,它类似于以下内容:

                    LIST-1  LIST-2        LIST-3              ... LIST-N
TIME                                           
2017-06-21 00:17:00 NaN     [99.221]       [42.357, 102.665]
2017-06-21 00:18:00 NaN     [50.89]        [42.357, 43.125,...]
2017-06-21 00:19:00 NaN     [61.50, 76.1]  [70.163, 121.486] 
2017-06-21 00:20:00 [70.16] NaN            NaN
2017-06-21 00:21:00 NaN     [102.665]      [57.9, 63.66, 68.7... 

每行代表一分钟的数据,list_N列的dtype代表对象。我想做:

  1. 将每行中的所有列表合并到一个浮点列表中,并将该列表用作新列 - ALL_LIST;
  2. 然后,将30分钟的数据((即30行数据 - ALL_LIST))合并到一个新列表中;
  3. 最后,我想得到一个这样的数据框:

    TIME                    LIST                                         
    2017-06-21 00:00:00   [99.221,42.357, 42.357, ...]
    2017-06-21 00:30:00   [52.328,42.357, 49.169, ...]
    2017-06-21 01:00:00   [61.484,42.357, 76.52, ...]
    2017-06-21 01:30:00   [76.523,42.357, 121.486, ...]
    

1 个答案:

答案 0 :(得分:1)

我为我的问题找到了一个解决方案。我会写出来并希望看看它是否能提高性能。

    all_tt_list['ALL_LIST'] = all_tt_list.apply(lambda x: ','.join(x.dropna()), axis=1)
    all_tt_list['ALL_LIST'] = all_tt_list['ALL_LIST'].astype(str).str.replace('[', '')
    all_tt_list['ALL_LIST'] = all_tt_list['ALL_LIST'].astype(str).str.replace(']', '')
    all_tt_list['ALL_LIST'] = all_tt_list['ALL_LIST'].astype(str).str.split(',')
    WAIT_TIME_INTERVAL = 30*60
    rng = pd.date_range(date, periods=(24 * 60 * 60 / WAIT_TIME_INTERVAL) + 1, freq=str(WAIT_TIME_INTERVAL) + 'S',
                    tz='Asia/Shanghai')
    for k in range(len(rng)):

        if(k == (len(rng)-1)):
            continue

        period_start = rng[k]
        period_end = rng[k+1]
        period_df = all_tt_list[all_tt_list.index > period_start]
        period_df = period_df[period_df.index < period_end]

        period_tt_list = period_df['ALL_LIST'].tolist()
        import itertools

        period_merged = list(itertools.chain.from_iterable(period_tt_list))

        period_merged_s = pd.DataFrame(period_merged, columns=['TT_NUM']).astype(float).astype(int)