具有字符串列到整数列表列的数据框

时间:2019-03-26 12:30:11

标签: python pandas dataframe

我有一个数据框,其中在一列中,每一行的数据都是这样的字符串:

[[25570], [26000]]

我希望系列中的每个条目成为整数列表。

IE:

[25570,26000]  ^^  int int

到目前为止,我可以将其保存到字符串列表中,但保留空白:

s = s.str.replace("[","").str.replace("]","")
    s = s.str.replace(" ","").str.split(",")

数据框字典:

     f =  {'chunk': {0: '[72]',
  1: '[72, 68]',
  2: '[72, 68, 65]',
  3: '[72, 68, 65, 70]',
  4: '[72, 68, 65, 70, 67]',
  5: '[72, 68, 65, 70, 67, 74]',
  6: '[68]',
  7: '[68, 65]',
  8: '[68, 65, 70]',
  9: '[68, 65, 70, 67]'},
 'chunk_completed': {0: '[25570]',
  1: '[26000]',
  2: '[26240]',
  3: '[26530]',
  4: '[26880]',
  5: '[27150]',
  6: '[26000]',
  7: '[26240]',
  8: '[26530]',
  9: '[26880]'},
 'chunk_id': {0: '72',
  1: '72-68',
  2: '72-68-65',
  3: '72-68-65-70',
  4: '72-68-65-70-67',
  5: '72-68-65-70-67-74',
  6: '68',
  7: '68-65',
  8: '68-65-70',
  9: '68-65-70-67'},
 'diffs_avg': {0: nan,
  1: 430.0,
  2: 335.0,
  3: 320.0,
  4: 327.5,
  5: 316.0,
  6: nan,
  7: 240.0,
  8: 265.0,
  9: 293.3333333333333},
 'sd': {0: nan,
  1: nan,
  2: 134.35028842544406,
  3: 98.48857801796105,
  4: 81.80260794538685,
  5: 75.3657747256671,
  6: nan,
  7: nan,
  8: 35.355339059327385,
  9: 55.075705472861024},
 'timecodes': {0: '[[25570]]',
  1: '[[25570], [26000]]',
  2: '[[25570], [26000], [26240]]',
  3: '[[25570], [26000], [26240], [26530]]',
  4: '[[25570], [26000], [26240], [26530], [26880]]',
  5: '[[25570], [26000], [26240], [26530], [26880], [27150]]',
  6: '[[26000]]',
  7: '[[26000], [26240]]',
  8: '[[26000], [26240], [26530]]',
  9: '[[26000], [26240], [26530], [26880]]'}}

1 个答案:

答案 0 :(得分:2)

尝试

f = pd.DataFrame().from_dict(s, orient='index')
f.columns = ['timecodes']
f['timecodes'].apply(lambda x: [a[0] for a in eval(x) if a])

输出

Out[622]:
0                                        [25570]
1                                 [25570, 26000]
2                          [25570, 26000, 26240]
3                   [25570, 26000, 26240, 26530]
4            [25570, 26000, 26240, 26530, 26880]
5     [25570, 26000, 26240, 26530, 26880, 27150]
6                                        [26000]
7                                 [26000, 26240]
8                          [26000, 26240, 26530]
9                   [26000, 26240, 26530, 26880]
10           [26000, 26240, 26530, 26880, 27150]
11                                       [26240]
12                                [26240, 26530]
13                         [26240, 26530, 26880]
14                  [26240, 26530, 26880, 27150]
15                                       [26530]
16                                [26530, 26880]
17                         [26530, 26880, 27150]
18                                       [26880]
19                                [26880, 27150]
Name: 0, dtype: object